10 Sep 2008 @ 11:39 AM 
JAVA:
  1. /*--------------------------------------------------------------------------
  2. ScanToArrayDemo class
  3. *****************
  4. By: HappyFace   http://www.engineeringserver.com
  5. Contact:        info [@] engineeringserver.com
  6. Version:        10/September/2008
  7. "*****************
  8. Note: Read a textfile into an ArrayList
  9. //----------------------------------------------------------------------*/
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Scanner;
  15.  
  16. public class ScanToArrayDemo {
  17. public static void main(String[] args){
  18. File fInput     = new File("c:\\input.txt");
  19. ArrayList textHolder = new ArrayList();
  20. String tmp = "";
  21. try {
  22. Scanner scInput = new Scanner(fInput);
  23. while (scInput.hasNextLine()){
  24. tmp = scInput.nextLine();
  25. textHolder.add(tmp);
  26. }
  27. }
  28. e.printStackTrace();
  29. }
  30. catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. //display the content of the array
  34. System.out.println(textHolder.get(3));
  35. }
  36. }

Tags Tags: , , , ,
Categories: Java
Posted By: HappyFace
Last Edit: 17 Sep 2008 @ 08 52 PM

E-mailPermalinkComments (0)
 03 May 2008 @ 11:24 PM 

A basic Java guessing game I made to show you how it could be done. You can specify the amount of numbers to guess and shows the total guesses you made to guess the correct number.

I've commented out the "continue" part because I'm too lazy to fix the small error that but if you uncomment the code you will see whats wrong. I suppose i'll fix that later.

Anyway here's the code:

JAVA:
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. public class GuessGame {
  4. String guessThisNumber = "";
  5. int[] secretNumber ;
  6. boolean guessedCorrectNumber = false, continueOrExitGame = false, debug = true;
  7. int tries = 0, difficulty, rounds = 1;
  8. //int difficulty;
  9. public static void main(String[] args) {
  10. GuessGame GG = new GuessGame();
  11. GG.usage();
  12. GG.playGame();
  13. }
  14. public void playGame(){
  15. Scanner sc = new Scanner(System.in);
  16. System.out.print("How many numbers you want to guess? ");
  17. try{
  18. difficulty = sc.nextInt();
  19. this.generateSecretNumber(difficulty);
  20. Scanner sd = new Scanner(System.in);
  21. while(!this.guessedCorrectNumber){
  22. System.out.print("Guess the secret number: ");
  23. String guess = sd.nextLine();
  24. if(guess.length()> difficulty || guess.length()
  25. System.out.println("Oops, please type " + difficulty  +  " numbers!");
  26. }
  27. if(guess.length() == difficulty){
  28. this.checkSecretNumber(guess);
  29. }
  30. this.tries++;
  31. if(guess.equals(this.guessThisNumber)){
  32. this.guessedCorrectNumber = true;
  33. System.out.println();
  34. System.out.println("Concratulations! the secret number is indeed " + this.guessThisNumber);
  35. System.out.println("Total tries: " + this.tries);
  36. System.out.println();
  37. /*
  38. Scanner quitGameChoice = new Scanner(System.in);
  39. System.out.print("Again? Y/N: ");
  40. String continueOrExit = quitGameChoice.nextLine();
  41. while(!continueOrExitGame){
  42. if(continueOrExit.equals("n") || continueOrExit.equals("N")){
  43. continueOrExitGame = true;
  44. System.out.println("Thanks for playing!");
  45. System.exit(0);
  46. }
  47. if(continueOrExit.equals("y") || continueOrExit.equals("Y")){
  48. continueOrExitGame = true;
  49. guessedCorrectNumber = false;
  50. tries = 0;
  51. rounds++;
  52. System.out.println("Alright! Ready for round " + rounds +  "?");
  53. System.out.println();
  54. this.playGame();
  55. }
  56. else{
  57. System.out.println("Please make a choice, again?: Y/N");
  58. continueOrExit = quitGameChoice.nextLine();
  59. }
  60. }
  61. */
  62. }
  63. }
  64. }
  65. catch(InputMismatchException IME){
  66. System.out.println("That is not a valid number");
  67. }
  68. }
  69. public void generateSecretNumber(int totalNumbers){
  70. guessThisNumber = "";
  71. secretNumber = new int[totalNumbers];
  72. for (int i = 0; i <totalNumbers; i++){
  73. int randomNumber = (int) (0+ Math.random()*10);
  74. secretNumber[i] = randomNumber;
  75. }
  76. for(int i = 0; i <secretNumber.length; i++){
  77. guessThisNumber +=secretNumber[i];
  78. //System.out.print(secretNumber[i]);
  79. }
  80. if(debug){
  81. System.out.println();
  82. System.out.println("Secret number is: " + guessThisNumber);
  83. }
  84. System.out.println("Alright, the secret number(s) are generated!");
  85. System.out.println();
  86. }
  87. public void checkSecretNumber(String userInput){
  88. int[] compareNumber = new int[userInput.length()];
  89. for (int i = 0; i <userInput.length(); i++){
  90. compareNumber[i] = userInput.charAt(i);
  91. }
  92. /*
  93. System.out.print(compareNumber[0]-48); // -48?
  94. */
  95. for (int i = 0; i <userInput.length(); i++){
  96. if(compareNumber[i]-48 <secretNumber[i]){
  97. System.out.print(compareNumber[i]-48 + "+ ");
  98. }
  99. if(compareNumber[i]-48> secretNumber[i]){
  100. System.out.print(compareNumber[i]-48 + "- ");
  101. }
  102. if(compareNumber[i]-48 == secretNumber[i]){
  103. System.out.print(compareNumber[i]-48 + "! ");
  104. }
  105. }
  106. System.out.println();
  107. }
  108. public void usage(){
  109. System.out.println("Welcome to the guessing game, guess the correct number!");
  110. System.out.println("By: HappyFace - www.engineeringserver.com :: v1.0 initial release");
  111. System.out.println("");
  112. System.out.println("Legenda: ");
  113. System.out.println("+ means higher");
  114. System.out.println("- means lower");
  115. System.out.println("! means correct");
  116. System.out.println("");
  117. }
  118. }

Output:

Welcome to the guessing game, guess the correct number!
By: HappyFace - www.engineeringserver.com :: v1.0 initial release

Legenda:
+ means higher
- means lower
! means correct

How many numbers you want to guess? 4

Secret number is: 6303
Alright, the secret number(s) are generated!

Guess the secret number: 6303
6! 3! 0! 3!

Concratulations! the secret number is indeed 6303
Total tries: 1

//edit
Oh and before I forget to mention it.. to disable the secretcode that shows on the screen just change "debug = true" into "debug = false" and the secretcode won't be visible.

Tags Tags: , , , ,
Categories: Java
Posted By: HappyFace
Last Edit: 24 Aug 2008 @ 06 27 PM

E-mailPermalinkComments (0)

Here's a demo I wrote some time ago to read a file and output it in the console. I've created 2 versions. One that uses the "Scanner object" and the other the "BufferedReader object" to read the text files from disk.

Both examples of mine work, but the Scanner version is preferred.

BufferedReader version:

JAVA:
  1. public class ReadFile {
  2. public static void main(String[] args) {
  3. ReadFile rf = new ReadFile();
  4. rf.readFile();
  5. }
  6. public void readFile(){
  7. try {
  8. FileReader fr = new FileReader("c:\\input.txt"); // read a file
  9. String tmp;
  10. tmp = br.readLine(); // read first line of file.
  11. while(tmp != null){ // read a line until end of file.
  12. System.out.println("" + tmp);
  13. tmp = br.readLine();
  14. }
  15. br.close();
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }


Scanner version:

JAVA:
  1. public class ReadLineDemo {
  2. public static void main(String[] args){
  3. ReadLineDemo EDS = new ReadLineDemo();
  4. EDS.readFile();
  5. }
  6. public void readFile(){
  7. String str;
  8. try {
  9. fr = new FileReader("c:\\input.txt");
  10. System.out.println("begin");
  11. Scanner sc = new Scanner(fr);
  12. while(sc.hasNext()){
  13. str = sc.nextLine();
  14. System.out.println(str);
  15. }
  16. }
  17. e.printStackTrace();
  18. }
  19. }
  20. }

Tags Tags: , , ,
Categories: Java
Posted By: HappyFace
Last Edit: 24 Aug 2008 @ 06 33 PM

E-mailPermalinkComments (0)

Here is a sample code I wrote to input and output text in a console. This is useful if you want input in your application.


public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input: ");
String text = sc.nextLine();
System.out.println("output: " + text);
sc.close();
}
}

Output:
Input: This is some input text that i typed
output: This is some input text that i typed

Tags Tags: ,
Categories: Java
Posted By: HappyFace
Last Edit: 14 Apr 2008 @ 01 57 PM

E-mailPermalinkComments (0)
\/ More Options ...