Java: Infinite Loop Using Scanner In.Hasnextint()

Java: Infinite loop using Scanner in.hasNextInt()

In your last else block, you need to clear the 'w' or other invalid input from the Scanner. You can do this by calling next() on the Scanner and ignoring its return value to throw away that invalid input, as follows:

else
{
System.out.println("You have entered an invalid input. Try again.");
in.next();
}

Java Scanner hasNextInt() causing infinite loop

Scanner.hasNextInt() skips over whitespace to find the next token. So it reads and skips over all your Enter presses. Then it waits for more input, because there may be more input coming.

In a Linux terminal, you can press Ctrl-D (maybe Cmd-D on OS X?) to send an end-of-file marker, which tells the application that there is no more input coming (this should be done at the start of a line). This answer suggests that Ctrl-Z is the equivalent in Windows' command prompt.

Alternatively, you could have some special input that your application reads. @phatfingers commented that you could specify the number of values to read as the very first input. You could also have a special value to signify the end (0 or -1 are common choices, based on the application's needs), or maybe even use a non-numeric token like "end".

Infinite loop using Scanner.hasNextInt

You need to consume the input from the Scanner

while (!scan.hasNextInt()) {
scan.next(); // consume non-integer values
}

Java, while infinite loop when using scanner

According to scanner oracle docs :
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

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.

So when you entered anything other than an integer, the scanner.nextInt() will not parse it to integer and throw InputMismatchException and the scanner will not move to the next token and tried to read again and again the same token.

To solve this, you can either change the loop or use hasNextInt() method or use scanner.next() in the catch block so that the scanner can move to the next token.

Infinite loop on .hasNext()

You have forgot to skip non int value so you are stuck in an infinite loop.

Try the code below.

int maxScore=0;
int score = 0;
Scanner scan = new Scanner("PacManHighScore");
while(scan.hasNext()) {
if(scan.hasNextInt()) {
score = scan.nextInt();
}else{
scan.next();
}
System.out.println(score);
if(score > maxScore) {
maxScore = score;
}
}
scan.close();

HasNextInt() Infinite loop

Based on your comment

A user can input multiple integers the amount is unknown. Ex: 3 4 5 1. There is a space inbetween them. All i want to do is read the integers and put it in a list while using two scanners.

You are probably looking for:

  • scanner which will read line from user (and can wait for next line if needed)
  • another scanner which will handle splitting each number from line.

So your code can look something like:

List<Integer> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.print("give me some numbers: ");
String numbersInLine = sc.nextLine();//I assume that line is in form: 1 23 45

Scanner scLine = new Scanner(numbersInLine);//separate scanner for handling line
while(scLine.hasNextInt()){
list.add(scLine.nextInt());
}
System.out.println(list);


Related Topics



Leave a reply



Submit