如标题中所述,我使用 BufferedReader 从 System.in 读取并将值放入字符串数组中。当我调用该方法来执行此操作时,我尝试检查数组的长度,但是当我这样做时,什么也没有打印出来。
String s = "";
BufferedReader io = new BufferedReader
(new InputStreamReader(System.in)); //create a new BufferedReader using system input
String line; //will be changed to the next line in the input
try //required or we cant compile it because exceptions
{
s = io.readLine() + "_"; //get the first integer value, which is how many
// pieces of info we have to process
while((line = io.readLine()) != null) //read until the end of the file
{
s += io.readLine() + "_"; //add it to the ArrayList (may not be needed)
//System.out.println("Added line to list");
//System.out.println(line);
}
io.close(); //done with input, close the stream
}
catch (IOException e)
{
e.printStackTrace();
}
return s.split("_");
然后我调用包含此代码的方法,但实际上没有打印任何内容:
String[] input = readInput();
System.out.println(input.length);
有什么建议吗?
最佳答案
您在循环内调用 readLine() 两次,并丢弃所有奇怪的结果。您需要分配 while 条件中获得的结果并在循环内使用它,而不是再次调用它。
关于java - 尝试使用 BufferedReader 从 System.in 读取输入并将其放入数组中,但似乎不想给我数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30271292/