简体   繁体   English

使用SCANNER读取用户输入的行

[英]Reading lines of input from user using SCANNER

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. 我试图使用Scanner类中的.hasNextLine()方法,但它没有返回false ....它再次等待输入。 Ive been looking around for a solution and it appears that .hasNextLine() waits for input, but i do not know what alternative to use. 我一直在寻找解决方案,似乎.hasNextLine()等待输入,但我不知道使用什么替代方案。 Any suggestions? 有什么建议么? The actual code looks like: 实际代码如下:

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

Thanks for your help 谢谢你的帮助

Perhaps you should use some sort of "stop" sequence meaning when the user enters a particular character sequence, it will break out the loop. 也许你应该使用某种“停止”序列,这意味着当用户输入特定的字符序列时,它会打破循环。 It might look something like: 它可能看起来像:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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