[英]java- use scanner to read in and process one piece of information at a time
我正在尝试使用扫描仪一次读取多行输入。 我认为使用循环的方式或读取信息的方式都有问题。 什么都没有存储在输出数组中。 我在代码中评论了我的问题。 有人可以帮忙吗? 谢谢!
Scanner c = new Scanner (System.in);
while (c.hasNextLine()){
ArrayList <Integer> record= new ArrayList <Integer> (); // next time the while loop runs, I expect the list record to be initialized again
String numbers = c.nextLine();
int n = Integer.parseInt(numbers);
for (int i=0; i<n; i++){
String info = c.nextLine();//then read the following n lines use for loop
int length = info.length(); //get the length of each line
record.add(length);
}
putRecordinArray(record);//some other function to process each record(a block of several lines processed each time by the while loop)
}
//here I tried to print out the array A generated by other function, but nothing was stored.
}
您的Arraylist名称为records
但您使用record
来调用它, record
是records without the 's'
。 您have to add the 's'
。 您的其中一个调用语句的示例。
record.add(length);
改成:
records.add(length);
也:
putRecordinArray(record);
改成:
putRecordinArray(records);
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.