fork download
  1. package hangman;
  2.  
  3. import java.util.Scanner;
  4. import java.io.File;
  5. import java.util.InputMismatchException;
  6. import java.io.FileNotFoundException;
  7. import java.util.Random;
  8.  
  9. public class Hangman {
  10.  
  11. private char letter;
  12. private String[] choices;
  13. private int amountOfGuesses = 6;
  14. private String word = random();
  15. private String guesses = numberOfLetters();
  16.  
  17. public static void main(String[] args) throws FileNotFoundException {
  18.  
  19. File inFile = new File("dictionary.txt");
  20. Scanner input = new Scanner(inFile);
  21. String[] choices = new String[500];
  22. int i =0;
  23. String line = input.nextLine();
  24. while (input.hasNextLine()){
  25. choices[i]=line;
  26. }
  27. input.close();
  28. }
  29.  
  30. public void Game() {
  31. System.out.println("Let's play Hangman!");
  32. System.out.println("This is your word: " + guesses);
  33. System.out.println("There are " + amountOfGuesses + " lives left.");
  34. while (amountOfGuesses > 0) {
  35. Scanner user = new Scanner(System.in);
  36. System.out.println("Please enter a letter: ");
  37. String idea = user.nextLine();
  38. letterCheck();
  39.  
  40. if (guesses.equals(word)) {
  41. System.out.println("You guessed the word: " + guesses);
  42. System.out.println("You win!!");
  43. break;
  44. }
  45. System.out.println("The word looks like this:" + guesses);
  46. System.out.println("You have " + amountOfGuesses + "left.");
  47. if (amountOfGuesses == 0) {
  48. System.out.println("The word was: " + word);
  49. System.out.println("You lose! Game over!");
  50. }
  51. }
  52. }
  53.  
  54. private String numberOfLetters() {
  55. String result = "";
  56. for (int i = 0; i < word.length(); i++) {
  57. result = result + "-";
  58. }
  59. return result;
  60. }
  61.  
  62. private String random() {
  63. Random game = new Random();
  64. int index = game.nextInt(choices.length);
  65. String guess = choices[index];
  66. return guess;
  67. }
  68.  
  69. private void letterCheck() {
  70.  
  71. if (word.indexOf(letter) == -1) {
  72. System.out.println("There are no " + letter + "'s in the word");
  73. amountOfGuesses--;
  74. }
  75. if (word.indexOf(letter) != -1) {
  76. System.out.println("The guess is correct.");
  77. }
  78. for (int i = 0; i < word.length(); i++) {
  79. if (letter == word.charAt(i)) {
  80. if (i > 0) {
  81. guesses = guesses.substring(0, i) + letter + guesses.substring(i + 1);
  82. }
  83. if (i == 0) {
  84. guesses = letter + guesses.substring(1);
  85. }
  86. }
  87. }
  88.  
  89. }
  90. }
  91.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class Hangman is public, should be declared in a file named Hangman.java
public class Hangman {
       ^
1 error
stdout
Standard output is empty