如标题所述,我使用从System读取的BufferedReader。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条件中获得的结果赋值,并在循环中使用它,而不是再次调用它。