Results 1 to 7 of 7
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Multiple lines in arrayList

    I have a little bit problems with putting multiple lines in an arrayList.
    The arrayList works, but he only takes 1 line in it.
    And I would like the arrayList to take all the lines in the file.

    Java Code:
     
    			// Create Scanner for file1.
    			// file1 bezit alleen de query= codes.
    			Scanner x = new Scanner(file1);
    
    			// We read the lines one by one in the file.
    			if (x.hasNextLine()) {
    				String line = x.nextLine();
    
    				// We put what is in file1 into an arrayList.
    				arrayList.add(line);
    
    			}

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Lund01,

    The if statement is only being evaluated once so will only execute the code once.
    Use a while loop instead.

    Regards.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    You keep asking this question.
    I answered elsewhere...you are only reading one line and inserting one line.
    You need a loop!

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I did use a while loop first.
    But the he read the 2nd file also 2 times.

    I it possible to put this while loop in another class?
    This one from arrayList.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    What?
    this is your code:
    Java Code:
    // Create Scanner for file1.
    			// file1 bezit alleen de query= codes.
    			Scanner x = new Scanner(file1);
    
    			// We read the lines one by one in the file.
    			[B]if [/B](x.hasNextLine()) {
    				String line = x.nextLine();
    
    				// We put what is in file1 into an arrayList.
    				arrayList.add(line);
    
    			}
    Why can you not simply:
    Java Code:
    // Create Scanner for file1.
                // file1 bezit alleen de query= codes.
                Scanner x = new Scanner(file1);
    
                // We read the lines one by one in the file.
                [B]while [/B](x.hasNextLine()) {
                    String line = x.nextLine();
                    // We put what is in file1 into an arrayList.
                    arrayList.add(line);
                }
    That will read each line (though I would use a BufferedReader, I hate Scanner for simple file reading) and stick it in an array.

  6. #6
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    This is funny....

    I first removed the while and made it an if, becose he also read the 2nd file two times. And that worked, he only read the 2nd file 1 time. And I just tried making the if to a while again. And now he reads it all into the arrayList. But the funny part is, he now also reads file2 only 1 time. While I haven't adjusted anything else. :confused:

    So it all works, but now for me to figure out why he first read file2 2 times. And now he doesn't anymore.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    That code is reading one file and one file only...file1.
    There is nothing in that code there involving a second file.

Similar Threads

  1. Deleting multiple lines of text from a file
    By barman in forum New To Java
    Replies: 4
    Last Post: 06-29-2010, 09:12 AM
  2. Replies: 12
    Last Post: 11-05-2009, 07:12 AM
  3. [SOLVED] Writing ArrayList to Text File on seperate Lines
    By shinjitsunohana in forum New To Java
    Replies: 9
    Last Post: 08-27-2008, 05:53 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •