I have a question regarding data validation and scanners.The following piece of code checks userinput.Anything other than an integer is not allowed and the user is asked to re-enter a value.My question is that the code works only if the scanner is declared within the while loop.The program executes infinitely if the scanner is declared outside.Why is that?Thanks.
int UserInp;
boolean dataType=false;
while(dataType==false)
{
Scanner sc=new Scanner(System.in);
try
{
System.out.print("\nEnter a number: ");
UserInp=sc.nextInt();
dataType=true;
}
catch(Exception JavaInputMismatch)
{
System.out.println("Option not available.Try again.");
}
}
Interesting problem!
What happens is that the Scanner attempts to translate the non-integer to an integer, and realizes it can't -- so it throws an InputMismatchException. However, it only advances past the token if the translation was successful.
Meaning, the invalid string is still in the input buffer, and it will fail the translation every single time you loop and try to call nextInt(). You never set dataType to true, and so you loop infinitely.
To see this in action, you can grab the arbitrary content in your catch block and print it out:
catch(Exception JavaInputMismatch){
System.out.println( sc.next() );
System.out.println("Option not available.Try again.");
}
Indeed, after invalid input, we get the following:
Enter a number: hello
hello
Option not available.Try again.
Enter a number:
And we don't loop infinitely. This is because the call to next() grabbed the value from the input buffer and advanced the scanner's pointer into that buffer to the next slot, which is now empty. So nextInt() will wait for input in that case.
Oh, and the reason it works fine if you initialize in the loop is that the scanner will always start reading input fresh; scanners don't share state across instances, so the "hello" that was in the buffer for the previous iteration isn't in the buffer for the next one due to the reinitialization.
Technically, it's still in the standard input buffer, but the scanner's pointer into that buffer is beyond the invalid string because it will start reading any new input, not existing input.
To add to Purag's answer, you could alternatively use nextLine() to advance the Scanner past the current line.
So your catch block will look like this:
catch(Exception JavaInputMismatch)
{
System.out.println("Option not available.Try again.");
sc.nextLine();
}
Tricky question.
You may get it!
The answer is simple. The Scanner object is kept live till the end of the execution as it is declared outside the while loop. Look this problem in the memory level.
The Scanner object is kept live so while entering the loop next time still the value(String value) will be there in Scanner object and it doesn't listens keyboard as the exception is already thrown.So the loop keeps going.
Note : The next() method in Scanner class will accept all the types of keyboard input but not the rest of the methods such as nextInt(), nextFloat() etc..,
Related
This is the program
public class bInputMismathcExceptionDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueInput = true;
do {
try {
System.out.println("Enter an integer:");
int num = input.nextInt();
System.out.println("the number is " + num);
continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (Incorrect input: an integer is required)");
}
input.nextLine();
}
while (continueInput);
}
}
I know nextInt() only read the integer not the "\n", but why should we need the input.nextLine() to read the "\n"? is it necessary?? because I think even without input.nextLine(), after it goes back to try {}, the input.nextInt() can still read the next integer I type, but in fact it is a infinite loop.
I still don't know the logic behind it, hope someone can help me.
The reason it is necessary here is because of what happens when the input fails.
For example, try removing the input.nextLine() part, run the program again, and when it asks for input, enter abc and press Return
The result will be an infinite loop. Why?
Because nextInt() will try to read the incoming input. It will see that this input is not an integer, and will throw the exception. However, the input is not cleared. It will still be abc in the buffer. So going back to the loop will cause it to try parsing the same abc over and over.
Using nextLine() will clear the buffer, so that the next input you read after an error is going to be the fresh input that's after the bad line you have entered.
but why should we need the input.nextLine() to read the "\n"? is it necessary??
Yes (actually it's very common to do that), otherwise how will you consume the remaining \n? If you don't want to use nextLine to consume the left \n, use a different scanner object (I don't recommend this):
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
input1.nextInt();
input2.nextLine();
or use nextLine to read the integer value and convert it to int later so you won't have to consume the new line character later.
Also you can use:
input.nextInt();
input.skip("\\W*").nextLine();
or
input.skip("\n").nextLine();
if you need whitespaces before line
I recently asked if there is any possible way whatsoever to get an exception from assigning a value to a String variable with the Scanner (The thread is here:)
And one of the guys told me that CTRL+D would be a case where a NoSuchElementException could be thrown. This to me is kind of a special case because input.nextLine() returns a String, and a String can be basically anything a user could type on the keyboard, so one would assume that input.nextLine() would not be a concern to throw an exception.
So I decided to add some try catch blocks into a program I'm writing on the off chance that CTRL+D is pressed when the program is asking for a number.
The problem I've run into is that when I catch the CTRL+D exception, the Scanner needs to be flushed, but if I flush the Scanner, it will cause a NoSuchElementException to occur because no new line exists. I'm using this all in a while true loop, so I'm kind of stuck between a rock and a hard place.
I will post one version of the code, with the input.nextLine() commented out. If you run it as is, you will get the infinite loop that happens when the Scanner needs to be flushed. If you uncomment the input.nextLine(), that very line of code will itself cause a NoSuchElementException.
import java.util.NoSuchElementException;
private int getMainOptions(){
System.out.printf("\n********** Main Options **********");
System.out.printf("\n*%32s*", "");
System.out.printf("\n* %-30s*", "[1] Create Customer");
System.out.printf("\n* %-30s*", "[2] Create Reservation");
System.out.printf("\n* %-30s*", "[3] Display Customer");
System.out.printf("\n* %-30s*", "[4] Display Reservation");
System.out.printf("\n*%32s*", "");
System.out.printf("\n**********************************");
while(true){
try{
System.out.print("\nChoose Option: ");
if(input.hasNextInt()){
return input.nextInt();
}
System.out.print("\nInvalid option");
input.nextLine();
continue;
}
catch(NoSuchElementException e){
System.out.print("\nAn exception occurred.");
//input.nextLine();
}
}
}
Apart from creating the Scanner inside the while loop and destoying it in the catch to be recreated in the next iteration, what can be done to solve this problem?
The code goes into an infinite loop when the input.nextLine() is commented out or when input.hasNextLine() is there to check because it is in a while(true) loop with nothing to stop it since input.nextInt() is not called. if(input.hasNextInt()) will not wait for an int, but simply skip the code inside the if statement if an int is not present as input.
Instead try this:
while(true){
try{
System.out.print("\nChoose Option: ");
String in=input.nextLine();
try{
int i=Integer.parseInt(in);
return i;
}catch(NumberFormatException ex)
{
System.out.print("\nInvalid option");
}
}
catch(NoSuchElementException e){
System.out.print("\nAn exception occurred.");
//input.nextLine();
}
}
}
I hope this helps and that I am understanding the question correctly
I'm not sure you fully grasp what happens when the user presses Ctrl-D. When that happens the standard input stream is closed. There is no way to reopen a closed stream. Even if you create a new Scanner and pass it in System.in it will still throw a NoSuchElementException.
As a Linux user if I press Ctrl-D in an interactive program, I expect the program to terminate. That is really all you can do at that point.
This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 7 years ago.
I am trying to write a method to handle all my input from the user in a console application. I call this method a total of 5 times. The first time, the condition is
a) the number must be positive and real-valued (double)
the next 4 times the condition is
b) the number must be greater than 1
This is my method:
private static double numChk(int errNum) {
final String[] ERROR_MESSAGE = {
"\nPlease enter only positive, real-valued numbers",
"\nPlease enter only positive numbers greater than 1" };
Scanner in = new Scanner(System.in);
double tempData;
while (!in.hasNextDouble()) {
System.out.println(ERROR_MESSAGE[errNum]);
System.out.print(" Please try again: ");
in.next();
}
tempData = in.nextDouble();
// in.close();
return tempData;
}
this is an example call to this method:
do {
System.out
.println("Please enter only positive, real-valued numbers");
System.out.print(" Enter a constant: ");
mu = numChk(0);
} while (mu <= 0);
note the "// in.close();" in the method I wrote. Without closing the Scanner in, this method works fine. However, my assignment requires me to make sure I close all open input streams. if I close the input stream in the method and re-open it, I get a NoSuchElementException. I know I could just put all of this into my main method and close the input at the end of it however, I would like to know if there is a way to do this (input validation, multiple times) and be able to close the input stream.
When you call in.close() you are also closing the System.in. The next time you try to use a new Scanner, it will try to use a closed System.in, and that is why you get an exception when you try to read from it
See:
Using Scanner.next() to get text input
If you call in.close() in the method, you need some way of determining if it is the last input or not.
I would recommend putting the scanner in your main method, passing the scanner in to the method, and then closing it when you're done with all your input.
What I suspect is happening is you're sending the input piped as if from a file. The first scanner buffers all the input, and when you close and re-open it, you end up losing the data that was buffered to the first scanner. I can't guarantee that, but that's what it looks like.
After while loop it tries to call, by this line there may not be any inputs.
tempData = in.nextDouble();
This question already has answers here:
try/catch with InputMismatchException creates infinite loop [duplicate]
(7 answers)
Closed 7 years ago.
I keep getting my code caught in an infinite while loop.
It is nothing to advanced, but i can not figure it out for the life of me!
Someone Please help
I have purplosely just re created the specific error without all of the if statements i have in my actual program.
package bs;
import java.util.InputMismatchException;
import java.util.Scanner;
public class bs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean continueVar = true;
while (continueVar) {
try {
System.out.println("Enter Something");
int input = sc.nextInt();
} catch (InputMismatchException i) {
System.out.println("What the f***?");
continueVar = true;
}
}
}
}
The infinite loop occurs when the Input mismatch exception is caught. I would think that it would atleast ask the user to re enter their input but instead of doing that it just continues in the loop as so:
run:
Enter Something
df
What the f***?
Enter Something
What the f***?
Enter Something
What the f***?
It acts like it is just ignoring the scanner object sc?!
No the scanner is not skipped, it's just starting at the beginning of the input. From the JavaDoc:
If the translation is successful, the scanner advances past the input that matched.
This means if the conversion isn't successfull the scanner won't advance. You'd thus have to manually skip the incorrect input using just next().
Edit: you might want to check for hasNextInt() before trying to read the input.
you loop while continueVar is true, but you never set to to false, so the loop never exits.
I think you want to set continueVar to false in the exception handler.
When a scanner throws an
InputMismatchException, the scanner
will not pass the token that caused
the exception, so that it may be
retrieved or skipped via some other
method.
The token that caused the mismatch is still in the scanner's buffer. You need to clear it before trying to scan again.
You can do that by calling next() in your catch block, like this:
catch (InputMismatchException i) {
System.out.println("What the f***?");
sc.next();
}
Also, you don't need to set continueVar to true again. You never set it to false, so it will stay true. Guessing that's an artifact of you removing this into a mini program.
Scanner does not advance when bad token is found. Look at Scanner.java, lines 2095-2096:
catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
Here's some sample code:
import java.util.Scanner;
class In
{
public static void main (String[]arg)
{
Scanner in = new Scanner (System.in) ;
System.out.println ("how many are invading?") ;
int a = in.nextInt() ;
System.out.println (a) ;
}
}
If I run the program and give it an int like 4, then everything goes fine.
On the other hand, if I answer too many it doesn't laugh at my funny joke. Instead I get this(as expected):
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at In.main(In.java:9)
Is there a way to make it ignore entries that aren't ints or re prompt with "How many are invading?" I'd like to know how to do both of these.
You can use one of the many hasNext* methods that Scanner has for pre-validation.
if (in.hasNextInt()) {
int a = in.nextInt() ;
System.out.println(a);
} else {
System.out.println("Sorry, couldn't understand you!");
}
This prevents InputMismatchException from even being thrown, because you always make sure that it WILL match before you read it.
java.util.Scanner API
boolean hasNextInt(): Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.
String nextLine(): Advances this scanner past the current line and returns the input that was skipped.
Do keep in mind the sections in bold. hasNextInt() doesn't advance past any input. If it returns true, you can advance the scanner by calling nextInt(), which will not throw an InputMismatchException.
If it returns false, then you need to skip past the "garbage". The easiest way to do this is just by calling nextLine(), probably twice but at least once.
Why you may need to do nextLine() twice is the following: suppose this is the input entered:
42[enter]
too many![enter]
0[enter]
Let's say the scanner is at the beginning of that input.
hasNextInt() is true, nextInt() returns 42; scanner is now at just before the first [enter].
hasNextInt() is false, nextLine() returns an empty string, a second nextLine() returns "too many!"; scanner is now at just after the second [enter].
hasNextInt() is true, nextInt() returns 0; scanner is now at just before the third [enter].
Here's an example of putting some of these things together. You can experiment with it to study how Scanner works.
Scanner in = new Scanner (System.in) ;
System.out.println("Age?");
while (!in.hasNextInt()) {
in.next(); // What happens if you use nextLine() instead?
}
int age = in.nextInt();
in.nextLine(); // What happens if you remove this statement?
System.out.println("Name?");
String name = in.nextLine();
System.out.format("[%s] is %d years old", name, age);
Let's say the input is:
He is probably close to 100 now...[enter]
Elvis, of course[enter]
Then the last line of the output is:
[Elvis, of course] is 100 years old
In general I really, really dislike using the same library call for both reading and parsing. Language libraries seem to be very inflexible and often just can't be bent to your will.
The first step that pulls data from System.in should not be able to fail, so have it read it as a string into a variable, then convert that string variable to an int. If the conversion fails, great--print your error and continue.
When you wrap your stream with something that can throw an exception, it gets kind of confusing just what state the whole mess leaves your stream in.
It's always a benefit to have your application throw an error when an error occurs opposed to ways to keep it from happening.
One alternative is to wrap the code inside a try {...} catch {...} block for InputMismatchException.
You might also want to wrap the code inside a while loop to have the Scanner keep prompting until a specific condition is met.