简体   繁体   中英

Program goes in an infinite loop

This program goes in an infinite loop in while cycle. Please, can someone tell me why?

import java.util.Scanner;
public class program {
    public static void main(String[] pars) {
        System.out.println("Insert something.");
        Scanner read = new Scanner(System.in);
        String s = "";
        while(read.hasNext()) {
            System.out.println(read.next());
        }
        System.out.println("End of program");
    }
}

Read the Javadoc of Scanner#hasNext() :

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan . The scanner does not advance past any input.

Hence the while loop will always be executed in your case, each time waiting for input from the user. Since the Scanner is linked to System.in , the input stream will always block until the user inputs a string and hasNext() will always return true, unless the user signals the end of file (eg through the Ctrl + z combination on Windows). Scanner#hasNext() is more convenient when reading from files where the input size is known and the end of the file marks the end of the stream.

One way to end the loop here is to add a condition on the input:

while (read.hasNext()) {
    s = read.next();
    if(s.equals("quit")) {
       break;
    }
    System.out.println(s);
}

PS : It is more conventional to name classes starting with an uppercase letter.

The problem is this line:

while(read.hasNext()) {

If you use System.in as a stream provided by the user, it will - if no such input is available - as @manouti says, block and wait for input. But even if you provide input, it will keep waiting. The system has no means to detect whether the user wants to provide additional input in the future.

It will only stop , if the Stream ends. This can be under two conditions:

  • The end of the file (in case of I/O redirection like java -jar program.jar < input.dat ; or
  • The user marks the end of a stream, in most shells with Ctrl+D. This marks the end-of-stream .

An alternative is to provide some kind of stop directive . Something like "END" . Thus:

while(read.hasNext()) {
    String nx = read.next();
    if(nx.equals("END")) {
        break;
    }
    System.out.println(nx);
}

Just remove while loop

public static void main(String[] pars) {
System.out.println("Insert something.");
    Scanner read = new Scanner(System.in);
    String s = "";

        System.out.println(read.next());

    System.out.println("End of program");
}

Or if u want display certain no.of string then mention condition properly.

public static void main(String[] pars) {
System.out.println("Insert something.");
    Scanner read = new Scanner(System.in);
    String s = "";
    int i=0;
    while(i<5) {
        System.out.println(read.next());
        i++;
    }
    System.out.println("End of program");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM