0

I am trying to use scanner to read in several lines of input at a time. I reckon that there is something wrong with either the way I use the loop, or the way I read in information. Nothing was stored in the output array. I commented my question in the code. Can someone please help? Thank you!

        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 Answers1

0

Your Arraylist name is records but you are calling it with record which is records without the 's'. You have to add the 's'. example of one of your calling statements.

record.add(length);

change to:

records.add(length);

also:

putRecordinArray(record);

change to:

putRecordinArray(records);

Hope this helps.

rert588
  • 737
  • 1
  • 7
  • 19