Results 1 to 7 of 7
Thread: Multiple lines in arrayList
- 10-15-2010, 10:53 AM #1
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); }
- 10-15-2010, 11:55 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Lund01,
The if statement is only being evaluated once so will only execute the code once.
Use a while loop instead.
Regards.
- 10-15-2010, 12:07 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You keep asking this question.
I answered elsewhere...you are only reading one line and inserting one line.
You need a loop!
- 10-15-2010, 12:09 PM #4
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.
- 10-15-2010, 12:12 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
What?
this is your code:
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]if [/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.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); }
- 10-15-2010, 12:17 PM #6
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.
- 10-15-2010, 12:34 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Deleting multiple lines of text from a file
By barman in forum New To JavaReplies: 4Last Post: 06-29-2010, 09:12 AM -
Accessing an arrayList of multiple items per index.
By scrap in forum New To JavaReplies: 12Last Post: 11-05-2009, 07:12 AM -
[SOLVED] Writing ArrayList to Text File on seperate Lines
By shinjitsunohana in forum New To JavaReplies: 9Last Post: 08-27-2008, 05:53 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks