2014-03-29 104 views
1

我是Java的新手,我试图从多行输入中获取字符串。从Java输入中获取字符串

例如字符串=“敏捷的棕色狐狸跳过懒狗敏捷的棕色狐狸跳过懒狗快速狐狸跃过懒狗。”从输入,就像这样:

敏捷的棕色狐狸跳过那只懒惰的狗。

快速的棕色狐狸跳过懒惰的狗。

快速的棕色狐狸跳过懒惰的狗。

我试图使用带有.nextLine(while循环)这样

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

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

但环只是看似无限延伸。感谢您提供任何帮助。

+0

需要有一个更好的退出,如果你按下Ctrl-Z程序不能得到更多的处理/程序将退出。添加一些System.out.printlns ...看到我编写的第二个示例,并可以运行 – tgkprog

回答

3

您必须按Ctrl键+ž(或按Ctrl +UNIX d),以表明不会有更多的投入。

+0

为什么这是downvoted?这是非常正确的答案! – Seelenvirtuose

+1

同意,如果OP在控制台上运行这个类,这是正确的答案。如果他用一个文件('java ClassName

+0

欢迎反馈! – Christian

0

需要结束一些标准的循环,加上你的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 
     } 
    } 
0

添加变量计数和增加它在每个looping.If计数满足一些标准,则可以打破;如循环播放。

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); 
2

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

所以,你应该只读的,你明确地知道将获得进入线,这样的:

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。)


编辑按欧文Bolwidt的评论基督教的回答:

如果要重定向标准输入流,所以它例如从一个文件时,EOF标记(文件末尾读取)会指示扫描仪输入的结束。所以显然不涉及用户交互,程序仍然会正确运行。