How to Get Out of While Loop in Java with Scanner Method "Hasnext" as Condition

How to get out of while loop in java with Scanner method hasNext as condition?

You keep on getting new a new string and continue the loop if it's not empty. Simply insert a control in the loop for an exit string.

while(!s1.equals("exit") && sc.hasNext()) {
// operate
}

If you want to declare the string inside the loop and not to do the operations in the loop body if the string is "exit":

while(sc.hasNext()) {
String s1 = sc.next();
if(s1.equals("exit")) {
break;
}
//operate
}

Java - How to break out of while with hasNext() condition?

The break; statement can be sued to... well... break out of an iteration. And by iteration I mean you can get out of a for too, for example.

You have to define WHEN do you want to break out of the iteration and then do something like this:

while(Input.hasNextInt(Input)){
if(condition())
break;

count++;

temp = Input.nextInt();
sum += temp;
System.out.println(temp);
System.out.println(count);

}

Otherwise, you can make an auxiliary method that defines if the iteration should keep on going, like this one:

private boolean keepIterating(Scanner in) {
boolean someOtherCondition = //define your value here that must evaluate to false
//when you want to stop looping
return Input.hasNextInt() && someOtherCondition;
}

Method that you will have to invoke in your while:

while(keepIterating()){

count++;

temp = Input.nextInt();
sum += temp;
System.out.println(temp);
System.out.println(count);

}

stop loops with hasNext() condition

if you only want 1 line read in 1 line and then parse the line

String line = Input.nextLine();
Scanner lineScanner = new Scanner(line);//Scanner can also work on a String instead of a InputStream
while(lineScanner .hasNext()){
if(lineScanner .hasNextFloat()){
Grade=lineScanner .nextFloat();
P.setGrades(Grade);
j=0;k=1;
continue;}
if(k==1){
P.getGPA();
P=new Person();
k=0;}
if(j==1){
P.setLastName(lineScanner .next());
continue;}
P.setFirstName(lineScanner .next());
i++;
j=1;
}

How to break out of while loop that involves hasNextLine()?

You could make this break from the loop on a blank line if you read the input as strings and then parse them into doubles.

while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.isEmpty()) {
break;
}
c.add(new Customer(Double.parseDouble(line)));
}

Alternatively you could use hasNextDouble() instead of hasNextLine() in your existing code. It is an error to mix hasNextLine() and nextDouble().

While loop with .hasNext condition running infinitely

Since your Scanner is handling opened stream (System.in) it can't be sure that this stream is not in the middle of creating and sending more data, which means it can't return false until

  • such stream will be closed
  • we will receive data representing end of stream.

Because of that

while(input.hasNext()){
...
}

is bad idea because you are blocked by hasNext() until you get proper data (or strim will be closed).

Proper solution depends on your scenario. If you know how many numbers you want to read simply use

for (int i = 0; i < N; i++){
...
rawInput = input.next();
...
}

In scenario in which you want to handle tokens from only one line you could first read that line and then tokenize it with separate Scanner

Scanner lineScanner  = new Scanner(input.nextLine());
while(lineScanner.hasNext()){
rawInput = lineScanner.next();
inputStorage.add(rawInput);
}

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.



Related Topics



Leave a reply



Submit