Reputation: 663
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("Guess a number betwwen 1 and 1000");
Random rand = new Random();
int secretNumber = rand.nextInt (1000);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You WON!!! Congratulations!");
else if (guess < secretNumber)
System.out.println("Nope, to low");
else if (guess > secretNumber)
System.out.println("Sorry, to high");
} while (guess != secretNumber);
}
}
how can i add to this code a statement, that IF NOT NUMERIC INPUT System.out.println("invalid input, please use type numbers only!")
Upvotes: 2
Views: 32388
Reputation: 3720
import java.util.Random;
import java.util.Scanner;
public class Game {
public static boolean isInteger( String input )
{
try
{
Integer.parseInt( input );
return true;
}
catch( Exception e)
{
return false;
}
}
public static void main(String[] args) {
System.out.println("Guess a number betwwen 1 and 1000");
Random rand = new Random();
int secretNumber = rand.nextInt (1000);
Scanner keyboard = new Scanner(System.in);
int guess=-1;
do {
String g = keyboard.next();
if(isInteger(g)){
guess = Integer.parseInt(g);
if (guess == secretNumber)
System.out.println("You WON!!! Congratulations!");
else if (guess < secretNumber)
System.out.println("Nope, to low");
else if (guess > secretNumber)
System.out.println("Sorry, to high");
}
else{
System.out.println("NaN");
}
} while (guess != secretNumber);
}
}
Upvotes: 0
Reputation: 610
public class Game {
public static void main(String[] args) {
System.out.println("Guess a number betwwen 1 and 1000");
Random rand = new Random();
int secretNumber = rand.nextInt (1000);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
if (!keyboard.hasNextInt()) {
System.out.println("invalid input, please use type numbers only!");
return;
}
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You WON!!! Congratulations!");
else if (guess < secretNumber)
System.out.println("Nope, to low");
else if (guess > secretNumber)
System.out.println("Sorry, to high");
} while (guess != secretNumber);
}
}
Upvotes: 0
Reputation: 2260
While using a scanner you will never figure out if it is an integer that has been entered or not. It will just wait until the "nextInt" is entered. What you can do is use the
Integer.parseInt() method. It will throw a NumberFormatException if the input string is not an integer.
Make guess a string and use. guess = keyboard.next();
Then use Integer.parseInt(guess) in a try-catch to solve your problem.
Upvotes: 0
Reputation: 12376
You can add a try catch block inside your loop.
do {
try{
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You WON!!! Congratulations!");
else if (guess < secretNumber)
System.out.println("Nope, to low");
else if (guess > secretNumber)
System.out.println("Sorry, to high");
}
catch(InputMismatchException e){
System.out.prinln("Not a number");
}
} while (guess != secretNumber);
Upvotes: 0
Reputation: 16149
I think you want the following wrapped around guess = keyboard.nextInt():
try
{
guess = keyboard.nextInt()
Integer.parseInt(guess);
<your if statements>
} catch(Exception ex)
{
System.out.println("Your comment");
}
Upvotes: 1
Reputation: 9331
Scanner.nextInt() throws
InputMismatchException
if the next token does not match the Integer regular expression, or is out of range
So you should wrap your code around a try-catch with this in mind
Upvotes: 3
Reputation: 726809
You should use Scanner
's hasNextInt()
method to determine if the input is numeric before calling nextInt
:
do {
while (!keyboard.hasNextInt()) {
System.out.println("Please enter only numbers.");
keyboard.next(); // Skip the wrong token
}
// Now that the input is valid, read the value:
guess = keyboard.nextInt();
// Put the rest of your logic here
...
} while (guess != secretNumber);
Upvotes: 6