0

I have a program that needs to read lines of input. It needs to be many lines at once. For example:


As I enter my time machine or maybe not, I wonder whether free will exists? I wonder whether free will exists maybe not as I enter my time machine or.


That all gets entered at one time by the user. I was trying to use .hasNextLine() method from Scanner class, but it is not returning false.... it waits for input again. Ive been looking around for a solution and it appears that .hasNextLine() waits for input, but i do not know what alternative to use. Any suggestions? The actual code looks like:

while(input.hasNextLine());
        {
            line += input.nextLine();
        }

Thanks for your help

4

1 回答 1

2

也许您应该使用某种“停止”序列,意思是当用户输入特定的字符序列时,它将中断循环。它可能看起来像:

public static void main(String args[]){
    final String stopSequence = "/stop";
    final Scanner reader = new Scanner(System.in);
    String input = reader.nextLine();
    while(!input.equalsIgnoreCase(stopSequence)){
        //process input
        input = reader.nextLine();
    }
}
于 2013-11-01T20:13:53.900 回答