从控制台java读取包含新行的字符串
我想读取此字符串(从控制台而不是文件),例如:从控制台java读取包含新行的字符串,java,stringreader,Java,Stringreader,我想读取此字符串(从控制台而不是文件),例如: one two three four five six seven eight nine public class ReadString { public static void main (String[] args) { // prompt the user to enter their name System.out.print("Enter your name: "); // open
one two three
four five six
seven eight nine
public class ReadString {
public static void main (String[] args) {
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Thanks for the name, " + userName);
}
} // end of ReadString class
所以我想每行读取它,并将每行放入一个数组中。
我怎么读呢?因为若我使用扫描仪,我只能读一行或一个字(下一行或下一行)
我的意思是阅读例如:一两trhee\n四五六\n七八九…
你应该自己做!
有一个类似的例子:
one two three
four five six
seven eight nine
public class ReadString {
public static void main (String[] args) {
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Thanks for the name, " + userName);
}
} // end of ReadString class
回答第一个答案评论中澄清的问题: 对于要读取的每一行,必须调用Scanner的nextLine()方法一次。这可以通过一个循环来完成。您将不可避免地遇到的问题是“我如何知道我的结果数组应该是大的?”答案是,如果不在输入本身中指定它,您将无法知道。您可以修改程序输入规范,要求行数如下:
3
One Two Three
Four Five
Six Seven Eight
然后,您可以使用以下命令读取输入:
Scanner s = new Scanner(System.in);
int numberOfLinesToRead = new Integer(s.nextLine());
String[] result = new String[numberOfLinesToRead];
String line = "";
for(int i = 0; i < numberOfLinesToRead; i++) { // this loop will be run 3 times, as specified in the first line of input
result[i] = s.nextLine(); // each line of the input will be placed into the array.
}
您可以使用以下代码读取输入:
Scanner s = new Scanner(System.in);
ArrayList<String> result = new ArrayList<String>();
String line = "";
while((line = s.nextLine()) != null) {
result.add(line);
}
Scanner s=新的扫描仪(System.in);
ArrayList结果=新建ArrayList();
字符串行=”;
而((line=s.nextLine())!=null){
结果。添加(行);
}
因此,与创建固定长度的数组不同,我们可以在输入中遇到ArrayList的每一行时,简单地将其.add()添加到ArrayList中。我建议您在尝试使用ArrayList之前阅读更多有关ArrayList的内容
tl;dr:使用循环为要读取的每一行调用next()或nextLine()
有关循环的更多信息:请查看以下代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class SearchInputText {
public static void main(String[] args) {
SearchInputText sit = new SearchInputText();
try {
System.out.println("test");
sit.searchFromRecord("input.txt");
System.out.println("test2");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void searchFromRecord(String recordName) throws IOException {
File file = new File(recordName);
Scanner scanner = new Scanner(file);
StringBuilder textFromFile = new StringBuilder();
while (scanner.hasNext()) {
textFromFile.append(scanner.next());
}
scanner.close();
// read input from console, compare the strings and print the result
String word = "";
Scanner scanner2 = new Scanner(System.in);
while (((word = scanner2.nextLine()) != null)
&& !word.equalsIgnoreCase("quit")) {
if (textFromFile.toString().contains(word)) {
System.out.println("The word is on the text file");
} else {
System.out.println("The word " + word
+ " is not on the text file");
}
}
scanner2.close();
}
}
那只能读一行我的意思如果读:一二三\n四五六\n七八九我真的很难理解你想要什么。假设您想将“一”、“二”、“三”读入一个数组,但每个单词都在同一行上,那么下一行包含“四”、“五”、“六”,并且您希望这些值中的每一个也在数组中,对吗?是的,每一行拆分一个文本,以便一二三将数组[0]四五六数组[1],等等。。。但是我甚至无法阅读文本,因为使用nextLine()只读取第一行,即1-2-3,那么我如何读取其他行?请尝试将readLine()放置一段时间,直到返回null。这是你的家庭作业,所以你需要学习