fork download
  1.  
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7.  
  8. /**
  9.  *
  10.  * @author joe
  11.  */
  12. public class Hangman {
  13.  
  14. public static void main(String[] args) throws IOException {
  15.  
  16. String fileName;
  17. Scanner reader;
  18. Scanner typist;
  19. Random generator = new Random();
  20. boolean completed;
  21. StringBuffer asterisk;
  22. String guess;
  23. String guesses;
  24. char again = 'n';
  25. char lett;
  26.  
  27. int timesMissed;
  28.  
  29. if (args.length == 0) {
  30. fileName = "worden.txt";
  31. } else {
  32. fileName = args[0];
  33. }
  34. //using filename to create a new file object
  35. java.io.File file = new java.io.File(fileName);
  36.  
  37. if (!file.exists()) {
  38. System.err.println("Error: File " + fileName + " does not exist.");
  39. System.exit(1);
  40. }
  41.  
  42. reader = new Scanner(file);
  43.  
  44. String[] wordArray = new String[15];
  45.  
  46. String word;
  47. int i=0;
  48. while (reader.hasNext()) {
  49. word = reader.next();
  50. wordArray[i] = word;
  51. i++;
  52. }
  53.  
  54. typist = new Scanner(System.in);
  55.  
  56. String worden = wordArray[(int) (Math.random() * wordArray.length)];
  57. do {
  58. completed = false;
  59. guesses = "";
  60. timesMissed = 0;
  61.  
  62. //create the asterisks
  63. asterisk = asteriskCover(worden);
  64.  
  65. while (!completed) {
  66. System.out.print("(Guess) Enter a letter in word " + asterisk +
  67. "> " );
  68. guess = typist.next();
  69.  
  70. //if you're weird and type out the whole thing
  71.  
  72. if (guess.length() > 1) {
  73. if (guess.equals(worden)) {
  74. System.out.println("Holy #$@%!, good job!");
  75. } else {
  76. System.out.println("Nope, that's not it.");
  77. }
  78. completed = true;
  79. //if you're not weird and just guess a letter like I
  80. // had originally asked you to.
  81. } else {
  82. lett = guess.charAt(0);
  83. guesses += lett;
  84. //2 lines below rep if letter isn't there and increments
  85. //times missed. (hopefully)
  86. if (worden.indexOf(lett) < 0) {
  87. timesMissed++;
  88. } else {
  89. //my awesome method to put the asterisk in
  90. rightLett(worden, asterisk, lett);
  91. }
  92. if (worden.equals(asterisk.toString())){
  93. System.out.println("The word is " + worden + " , you missed "
  94. + timesMissed + " time(s)");
  95. System.out.print("Do you want to guess another word?"
  96. + " Enter y or n > ");
  97. again = typist.next().charAt(0);
  98.  
  99. }
  100. }
  101. }
  102. }
  103. while(again == 'Y' || again == 'y');
  104. reader.close();
  105. }
  106.  
  107.  
  108.  
  109. public static StringBuffer asteriskCover(String a) {
  110. StringBuffer asterisk = new StringBuffer(a.length());
  111. for (int count = 0; count < a.length(); count++) {
  112. asterisk.append('*');
  113. }
  114. return asterisk;
  115. }
  116.  
  117. public static void rightLett(String worden, StringBuffer asterisk, char lett) {
  118. for (int dex = 0; dex < worden.length(); dex++) {
  119. if (worden.charAt(dex) == lett) {
  120. asterisk.setCharAt(dex, lett);
  121. }
  122. }
  123. }
  124. }
  125.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:12: error: class Hangman is public, should be declared in a file named Hangman.java
public class Hangman {
       ^
1 error
stdout

Standard output is empty