Reputation: 1
I'm trying to create some input validation for incorrect input in my program.
The program is supposed to read the user input as a double but it must display a message that input is not a number if the user types a letter instead of a number.
To do this, I use input.hasNextDouble with an if statement. The else, prints the message that x is not a number. However, my problem lies with the second number that the user inputs. Let's say that the user enters "5 f" The hasNextDouble interprets the double and then executes the code under the if statement, but the input is still incorrect because of the "f".
In short, I need a way for the first input (5) to be removed from the buffer so the second input can be interpreted.
Any help is appreciated!
here is some sample code:
if ( keyboard.hasNextDouble() ) {
double i = keyboard.nextDouble();
double j = keyboard.nextDouble();
double answer = (j+i );
System.out.println(answer);
}
else {
String a = keyboard.next();
String b = keyboard.next();
System.out.println( a + "is not a number");
Upvotes: 0
Views: 1962
Reputation: 124215
You can create helper method which will ask user for valid input, until it gets one, or until some limit of tries will be exceeded. Such method can look like this:
public static double getDouble(Scanner sc, int maxTries) {
int counter = maxTries;
while (counter-- > 0) {// you can also use `while(true)` if you
// don't want to limit numbers of tries
System.out.print("Please write number: ");
if (sc.hasNextDouble()) {
return sc.nextDouble();
} else {
String value = sc.next(); //consume invalid number
System.out.printf(
"%s is not a valid number (you have %d tries left).%n",
value, counter);
}
}
throw new NumberFormatException("Used didn't provide valid float in "
+ maxTries + " turns.");
}
then you can use this method like
Scanner keyboard = new Scanner(System.in);
double i = getDouble(keyboard, 3);
double j = getDouble(keyboard, 3);
double answer = (j + i);
System.out.println(answer);
Upvotes: 0
Reputation: 96
double i,j, answer;
try{
i = keyboard.nextDouble();
j = keyboard.nextDouble();
answer = i+j;
} catch( InputMismatchException e ) {
// One of the inputs was not a double
System.out.println("Incorrect format");
}
otherwise if you absolutely need to print out which was incorrect, I would just do the if-then you have twice.
double i=null, j=null, answer;
// get input for i
if ( keyboard.hasNextDouble() ) {
i = keyboard.nextDouble();
}else{
System.out.println( keyboard.next() + " is not a double");
}
// get input for j
if ( keyboard.hasNextDouble() ) {
j = keyboard.nextDouble();
}else{
System.out.println( keyboard.next() + " is not a double");
}
// if both i and j received inputs
if( i != null && j != null )
answer = i + j;
else
System.out.println("Malformed input");
Upvotes: 1
Reputation: 7057
You need something like following.
double tryReadDouble(Scanner keyboard) throws NumberFormatException {
if (keyboard.hasNextDouble()) {
return keyboard.nextDouble();
} else {
throw new NumberFormatException(keyboard.next());
}
}
And
try {
double i = tryReadDouble(keyboard);
double j = tryReadDouble(keyboard);
double answer = (j + i);
System.out.println(answer);
} catch (NumberFormatException ex) {
System.out.println(ex.getMessage() + "is not a number");
}
Hope this helps.
Upvotes: 0