java-使用扫描程序一次读入并处理一条信息

时间:2014-09-25 09:59:20

标签: java loops arraylist while-loop system.in

我正在尝试使用扫描仪一次读入几行输入。我认为使用循环的方式或我读取信息的方式都有问题。输出数组中没有存储任何内容。我在代码中评论了我的问题。有人可以帮忙吗?谢谢!

        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. 
    }

1 个答案:

答案 0 :(得分:0)

您的Arraylist名称为records,但您使用record records without the 's'来呼叫它。你have to add the 's'。 你的一个调用语句的例子。

record.add(length);

更改为:

records.add(length);

也:

putRecordinArray(record);

更改为:

putRecordinArray(records);

希望这有帮助。