0

I have developed a hangman game which is 99% complete. I just have one glitch and the glitch is when the user does not enter in a guess and presses enter.

I know that we cannot check if a character is empty so instead I check if a string is empty using scanner.nextLine().isEmpty(). Now if I don't enter anything and I press enter then it works fine and outputs the printline message.

However if I enter in a letter and press enter, then it goes to the next line and it wants me to enter in something. If I enter in another letter then it works fine, if I enter in nothing and press enter then I get an exception where

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

So I believe my problem is the if statement where I check for string to be empty and if not then move onto checking the character:

            if (scanner.nextLine().isEmpty()) {
                System.out.println("Your guess seems empty. Please enter in a letter or number");
            } else {

                char input = scanner.nextLine().charAt(0);

                if (input == '-' || Character.isLetter(input) || Character.isDigit(input)) {

                    if (input == '-') {
                        weArePlaying = false;
                        wordCompleted = true;
                    }
....
}

How can I jiggle this so that if empty string then ask user to guess again else if not empty then proceed with the guess?

Below is more of the code:

public static void main(String[] args) throws FileNotFoundException {

    ArrayList<String> movieList = new ArrayList<>();
    File file = new File("C:\\Users\\mmkp1\\Documents\\listofmovies.txt");
    Scanner fileScanner = new Scanner(file);
    int attempts = 10;
    Scanner scanner = new Scanner(System.in);
    Random random = new Random();
    ArrayList<Character> guessedLetters = new ArrayList<>();

    while (fileScanner.hasNextLine()) {
        String line = fileScanner.nextLine();
        // Reads the whole file
        movieList.add(line);
    }

    boolean weArePlaying = true;
    while (weArePlaying) {
        System.out.println("Welcome to Guess The Movie Game");

        int index = random.nextInt(movieList.size());
        String getMovies = movieList.get(index);
        char[] randomWordToGuess = getMovies.toLowerCase().toCharArray();
        int wordLength = randomWordToGuess.length;
        char[] playerGuess = new char[wordLength];
        boolean wordCompleted = false;

        for (int i = 0; i < playerGuess.length; i++) {
            playerGuess[i] = '_';
        }

        for (int i = 0; i < randomWordToGuess.length; i++) {
            if (randomWordToGuess[i] == ' ') {
                playerGuess[i] = ' ';
            }
        }


        while (!wordCompleted && attempts != 0) {

            printArray(playerGuess);
            System.out.println("Number of attempts left: " + attempts);
            System.out.println("Your previous guesses were:" + guessedLetters);
            System.out.println("Enter a letter or number");

            if (scanner.nextLine().isEmpty()) {
                System.out.println("Your guess seems empty. Please enter in a letter or number");
            } else {

                char input = scanner.nextLine().charAt(0);

                if (input == '-' || Character.isLetter(input) || Character.isDigit(input)) {

                    if (input == '-') {
                        weArePlaying = false;
                        wordCompleted = true;
                    }
....
}
Merlin
  • 185
  • 2
  • 14

1 Answers1

2

You are using the scanner wrong and calling nextLine() twice.

if (scanner.nextLine().isEmpty()) {            // First call.
    System.out.println("empy");
}
else {
    char input = scanner.nextLine().charAt(0); // Second call.
}

Let's say the scanner has an interesting line for us. The first call will get it, see that it is not empty and throw it away. The second call will not get our interesting line but will attempt to get a (possibly empty) following line.

Trying to access the first character of an empty string results in the StringIndexOutOfBoundsException you posted.

You can easily fix your code with the help of a local variable like so:

String line;
char   input;

line = scanner.nextLine();
if (line.isEmpty()) {
    System.out.println("empy");
    return;
}

input = line.charAt(0);
// ...
lupz
  • 3,620
  • 2
  • 27
  • 43
  • That worked, only thing I need to work out is not to end the game after I press enter as it displays the message and then the console seems to finish – Merlin May 09 '18 at 15:07