I wrote this little guessing game where you have to guess of cause a random number. I finished this programm but the last thing I would like to to do is: When the user should input a number and its and InputMissmatching error or other errors, the System tells him that he has to input a number taller than 0. But the variable of the Scanner doesn't work after this do-while loop anymore! How to I have to set up the try/catch/finally with a loop to get only integers and not crash when its not an integer or 0?
The problem is that the guessNumber and the numberOfUser variables havent't been initialized now! lines: 37,53 so what do i have to change in the try/catch loop?
package gessinggame;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class GessingGame {
public static void main(String[] args) {
//<editor-fold desc="Start Variables and Objects">
Random random = new Random();
Scanner input = new Scanner(System.in);
int counter = 0;
int numberOfUser;
int guessNumber;
int x = 1;
System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
do{
try{
numberOfUser = input.nextInt();
guessNumber = random.nextInt(numberOfUser);
x = 2;
}catch(Exception e)
{
System.out.print("That doesn't work! Please enter an integer higher than 0 :");
x = 1;
}
}while(x == 1);
//</editor-fold>
//<editor-fold desc="do-while loop (higher/lower/equal)">
System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
System.out.println("---------------------------------------------------");
do{
int userguess = input.nextInt();
if(userguess > numberOfUser)
{
System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
}
else{
if(userguess < 0)
{
System.out.println("My number is always taller than 0");
} else{
if(guessNumber < userguess)
{
System.out.println("lower");
counter++;
}
if(guessNumber > userguess)
{
System.out.println("higher");
counter++;
}
if(userguess == guessNumber)
{
counter++;
break;
}
}
}
}while(true);
//</editor-fold>
//<editor-fold desc="print final result">
System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");
//</editor-fold>
}
}
Add input.nextLine() in your try/catch
blocks.
call Scanner.nextLine
call after each Scanner.nextInt
to consume rest of that line including newline.
package gessinggame;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class GessingGame {
public static void main(String[] args) {
//<editor-fold desc="Start Variables and Objects">
Random random = new Random();
Scanner input = new Scanner(System.in);
int counter = 0;
int numberOfUser = 0;
int guessNumber = 0;
int x = 1;
System.out.print("Try to guess my number! What should be the highest number I can go up to?: ");
do{
try{
numberOfUser = input.nextInt();
guessNumber = random.nextInt(numberOfUser);
x = 2;
}catch(Exception e)
{
System.out.print("That doesn't work! Please enter an integer higher than 0 :");
input.nextLine();
x = 1;
}
}while(x == 1);
System.out.println("Try to guess my number! Its from 0 to " + numberOfUser + " !");
System.out.println("---------------------------------------------------");
do{
int userguess = input.nextInt();
if(userguess > numberOfUser)
{
System.out.println("Oh man, at the beginning you said that the highest number is: " + numberOfUser);
}
else{
if(userguess < 0)
{
System.out.println("My number is always taller than 0");
} else{
if(guessNumber < userguess)
{
System.out.println("lower");
counter++;
}
if(guessNumber > userguess)
{
System.out.println("higher");
counter++;
}
if(userguess == guessNumber)
{
counter++;
break;
}
}
}
}while(true);
System.out.println("You guessed my number! My number was: " + guessNumber + "! You needed " + counter + " attempts!");
}
}