Reputation: 284
So, I have this situation: I have two .csv files and I need to read and save data for both of them.
The problem is here:
while ((nextPM = csvReader2.readNext()) != null) {
System.out.println(nextPM[0]);
while ((nextRecord = csvReader.readNext()) != null) {
System.out.println(nextRecord[0] + "asd");
if(nextRecord[0].equals(nextPM[0])) {
System.out.println(nextRecord[0] + " " + nextRecord[1] + " " + nextPM[2]);
}
}
}
First time works perfectly, but when the first while loop start again, the second while is just skipped. Any solution? Of course nextPM
and nextRecord
are String[]
(initialized outside of the code I'm showing to you)
Upvotes: 0
Views: 1022
Reputation: 1
Store the data of 1st file in the ArrayList. Start iterating the 2nd file using the While loop and use the iteration of the ArrayList using the For-loop in the While loop.
This is what worked for me.
Upvotes: 0
Reputation: 16498
You can just move the declaration and initializiation of your csvReader
in to the first while loop just above the inner loop starts. Doing so you create for each iteration of the inner loop a new reader:
while ((nextPM = csvReader2.readNext()) != null) {
System.out.println(nextPM[0]);
CSVReader csvReader = new CSVReader(new FileReader("yourfile.csv"));
while ((nextRecord = csvReader.readNext()) != null) {
System.out.println(nextRecord[0] + "asd");
if(nextRecord[0].equals(nextPM[0])) {
System.out.println(nextRecord[0] + " " + nextRecord[1] + " " + nextPM[2]);
}
}
}
Upvotes: 0
Reputation: 102842
The inner while loop will continue to read records from csvReader
until there are no records left to read.
csvReader
does not 'reset' just because the while loop ended: The reader is done. Therefore, the next time you try to read records from it, it immediately returns; csvReader
is still at the end.
To solve this problem, you would have to somehow reset csvReader
back to the beginning. Most stream-style java APIs intentionally do not support reset
(because many sources of data do not have the notion 'please start from the beginning again'). Therefore, to 'reset' it you would have to recreate it inside the outer loop, thus guaranteeing you have a fresh new csvReader
every time you loop.
If your source material is a file, this might be slow. If it's a network connection, this is not possible. But, there's another solution: You can read the records from csvReader
once through and save these in memory, and then go by your memory copy. This is not feasible if the source is very large (you'd need a lot of RAM), but assuming it isn't:
// Save all records in csvReader into a list.
List<String[]> allRecords = new ArrayList<>();
while ((String[] record = csvReader.readNext()) != null) allRecords.add(record);
and then use this list in your inner loop:
while ((nextPM = csvReader2.readNext()) != null) {
System.out.println(nextPM[0]);
for (String[] nextRecord : allRecords) {
System.out.println(nextRecord[0] + "asd");
if(nextRecord[0].equals(nextPM[0])) {
System.out.println(nextRecord[0] + " " + nextRecord[1] + " " + nextPM[2]);
}
}
}
Upvotes: 1
Reputation: 51892
When the inner loop has been completed for the first iteration of the outer loop the cvsReader
will have reached EOF so any further calls csvReader.readNext()
will return null.
One solution is to not nest the loops but first read one of the files into an array or other collection and then work against that array when reading the second array.
Upvotes: 1