I do not understand why I get this error. When I run the code below, it runs the way it should, right up until when it asks me if I would like to play another game, and then I do not have a chance to answer and I get the error.
package src.GuessingGame;
import java.util.Scanner;
public class GuessingGame {
//constants
private static final int MIN_NUMBER = 1;
private static final int MAX_NUMBER = 100;
//stats
private static int totalGames = 0;
private static int totalGuesses = 0;
private static int bestGame = Integer.MAX_VALUE;
private static int guesses;
private static int guess;
private static Scanner guessScanner = new Scanner(System.in);
private static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
String option = "Y";
introduce();
//while user keeps answering 'yes'
while(option.toUpperCase().startsWith("Y"))
{
playOneGame();
System.out.println("Would you like to play another game? (y or n) ");
option = in.nextLine();
}
printResults();
in.close();
}
private static void introduce()
{
System.out.println("This program allows you to play a guessing game. \nI will think of a number between "
+ MIN_NUMBER + " and " + MAX_NUMBER + " and will allow you to guess until you get it. \nFor each guess, I will tell you whether the right answer is higher or lower than your guess");
}
private static void playOneGame()
{
guesses = 0;
//generates random number
int number = (int)(Math.random() * MAX_NUMBER + MIN_NUMBER);
System.out.println("I'm thinking of a number between " + MIN_NUMBER + " and " + MAX_NUMBER + "...");
//print out random number for debugging
System.out.println(number);
System.out.print("Your guess? ");
validInput();
guesses++;
//while user guesses wrong
while(guess != number)
{
if(guess > number)
{
System.out.println("It's lower.");
guesses++;
}
else if(guess < number)
{
System.out.println("It's higher.");
guesses++;
}
//if guess = random number
else
{
guess++;
break;
}
System.out.print("Your guess? ");
validInput();
}
if(guesses == 1)
{
System.out.println("You got it right in " + guesses + " guess");
}
else
{
System.out.println("You got it right in " + guesses + " guesses");
}
totalGuesses += guesses;
totalGames++;
if(guesses < bestGame)
{
bestGame = guesses;
}
guessScanner.close();
}
private static void printResults()
{
System.out.println("Overall results: ");
System.out.println("total games = " + totalGames);
System.out.println("total guesses = " + totalGuesses);
System.out.println("guesses/game = " + (double)((double)totalGuesses / (double)totalGames ));
System.out.println("Best game = " + bestGame);
}
private static void validInput()
{ //while user inputs non ints
while (!guessScanner.hasNextInt())
{
System.out.println("Invalid data, try again");
guessScanner.next();
}
guess = guessScanner.nextInt();
}
}
Here is the output:
This program allows you to play a guessing game.
I will think of a number between 1 and 100 and will allow you to guess until you get it.
For each guess, I will tell you whether the right answer is higher or lower than your guess
I'm thinking of a number between 1 and 100...
42
Your guess?
42
You got it right in 1 guess
Would you like to play another game? (y or n)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at LetterCounter/src.GuessingGame.GuessingGame.main(GuessingGame.java:29)
I've looked at other posts about this and tried the things in them, but none of them seemed to work. I'm coding in Visual Studio Code and using JDK 17.0.1, if that helps. I'm still pretty new to Java programming, so I could be missing something pretty obvious, but I haven't been able to find anything on this so far. I have also tried this in Eclipse and IntelliJ to see if there is something wrong with the Java in VSCode, but the same result occurs. Thank you in advance!