首页 文章

从Java中的输入中获取字符串

提问于
浏览
1

我是Java的新手,我正在尝试从多行输入中获取字符串 .

例如一根绳子=“快速的棕色狐狸跳过懒狗 . 快速的棕色狐狸跳过懒狗 . 快速的狐狸跳过懒狗 . ”来自输入,如下:

快速的棕色狐狸跳过懒狗 . 敏捷的棕色狐狸跳过了懒狗 . 敏捷的棕色狐狸跳过了懒狗 .

我试过像这样使用带有.nextLine()的while循环

String s="";
Scanner Input = new Scanner (System.in);

while(Input.hasNextLine()){
    s = s + Input.nextLine() + " ";
}

但这个循环看似无限地运行 . 感谢您提供的任何帮助 .

4 回答

  • 0

    您必须按Ctrl z(或UNIX上的Ctrl d)以指示不会有更多输入 .

  • 0

    如果您没有指明,标准输入流通常永远不会结束 . 您的程序如何知道用户没有输入另一行?

    因此,您应该只阅读您明确知道会输入的行,如下所示:

    Scanner input = new Scanner(System.in);
    String first = input.nextLine();
    String second = input.nextLine();
    input.close();
    System.out.println(first);
    System.out.println(second);
    

    如果你想拥有一个可变数量的行,你应该使用你的while循环,但是用户必须指出输入的结束,正如Christian建议的那样 . 像那样:

    Scanner input = new Scanner(System.in);
    StringBuilder builder = new StringBuilder();
    while (input.hasNext()) {
        builder.append(input.nextLine()).append(System.lineSeparator());
    }
    input.close();
    System.out.println(builder.toString());
    

    运行会无限循环(貌似) . 但是当按下CTRL-Z时你将结束输入, input.hasNext() 将返回false . (注意,CTRL-Z适用于Linux和Windows . )


    根据Erwin Bolwidt对Christian的答案的评论编辑:

    如果要重定向标准输入流,以便例如从文件读取,则EOF标记(文件末尾)将指示扫描仪输入的结束 . 因此显然没有涉及用户交互,程序仍然会正常运行 .

  • 2

    添加一个变量计数并在每个循环中递增它 . 如果计数符合某些条件,那么你可以打破;来自循环 . 某些人喜欢,

    String s = "";
        Scanner Input = new Scanner(System.in);
        int count = 0;
    
        while (Input.hasNextLine()) {
            if (count == 4) {
                break;
            } else {
                count++;
                s = s + Input.nextLine() + " ";
            }
    
        }
        System.out.println(s);
    
  • 3

    需要在某些条件上结束循环,加上你的var名称是1个字符,这是不好的 .

    你的方式工作按ctrl -Z(窗口)结束循环,但我不认为可以在此之后从标准输入输入更多输入 .

    import java.util.*;
        public class ReadStrs{
            public static void main(String []args){
                String s = "";
                String readStr = "";
                StringBuilder sb = new StringBuilder();
                Scanner input = new Scanner (System.in);
                int cnt = 0;
                //while( !(readStr = input.nextLine()).equals("end"))
                System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
                while(input.hasNextLine()){
                    cnt++;
                    readStr =input.nextLine();
    
                    sb.append(readStr).append(" ");
                    System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
                 }//this will go on for ever unless you have a check to break the input and start processing
                 s = sb.toString();//can manipulate sb or s now
                 System.out.println("Out of loop got :" + s + "---");
            }
         }
    

    另一种方法是通过输入特定字符串来提示用户结束输入:

    import java.util.*;
        public class ReadStrs{
            public static void main(String []args){
                String s = "";
                String readStr = "";
                StringBuilder sb = new StringBuilder();
                Scanner input = new Scanner (System.in);
                int cnt = 0;
                while( !(readStr = input.nextLine()).equals("end")){
                    cnt++;
                    System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
                    sb.append(readStr).append(" ");
                 }//this will go on for ever unless you have a check to break the input and start processing
                 s = sb.toString();//can manipulate sb or s now
            }
         }
    

相关问题