r/javahelp • u/bei60 • Mar 23 '20
Parsing a CSV file - finding a specific field in the same row
Hello guys,
So I'm searching for a way to find a specific string in my CSV, and once found, search another item in that same row. For example:
| itemID | item name | quantity |
|---|---|---|
| 001 | item1 | 3 |
| 002 | item2 | 2 |
I have this piece of code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVParser {
static void findProduct(String keyword) {
String line = "";
String splitBy = ",";
try {
BufferedReader br = new BufferedReader(new FileReader("c:\\temp\\test.csv"));
while ((line = br.readLine()) != null) {
String[] prod = line.split(splitBy);
if (prod[1].contains(keyword)){
System.out.println(keyword + " exists");
return;
}
else{
System.out.println(keyword + " is not in stock");
}
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
findProduct("item1");
}
}This code will return that item1 exists, because it searches for it in prod[1]. Once found, I'd like to know the quantity of item1 by skipping to the next field. So I can do prod[2] right?
My question is, how do I parse the rest of the items? The for loop goes over everything, and my if (contains()) can find the existence of an item, but then what? How do I access other fields in that same row where the String was found?
Thanks :)
1
u/Kambz22 Intermediate Brewer Mar 23 '20
Like this? Once you found your row, you can loop through your split array for the rest of the elements in that row.
if (prod[1].contains(keyword))
{
//Loop through the array
{
//No you can get fields in that row, by saying prod[i]
}
}1
u/bei60 Mar 23 '20
Nice, it did work! I couldn't figure out how to access just the quantity field though. The loop goes through the whole row.
1
Mar 23 '20
[removed] — view removed comment
1
u/bei60 Mar 24 '20
Thanks!
I'm a beginner so sorry for not understanding straight away. I have some questions:
final BufferedReader br = new BufferedReader(new FileReader("c:\\temp\\test.csv"));- Here you're loading the CSV file.
final List<String> columns = Arrays.stream(br.readLine().split(splitBy)).map(String::trim).collect(Collectors.toList())- Here you're loading the number of columns to a list?
final int nCols = columns.size();- This one takes the size of the list that we got from the previous statement?
while ((line = br.readLine()) != null) {- Here we're looping over the file, and it will continue to do so as long as it's not null.
if (prod[1].trim().contains(keyword)) {- If the keyword I'm searching for is found, then we're going in
for (int i = 2; i < nCols; i++)- Couldn't quite figure out what this loop is for. What if I want to get the quantity of the product I found? You assumed correct that the CSV is fixed and I always know the number of the quantity column. You went by the example in the thread and initialized thei=2to start in the 3rd column?Thanks again :)
3
u/ItsKoku Mar 24 '20
You could also consider using Apache Commons CSV: https://commons.apache.org/proper/commons-csv/
It allows you to easily parse a CSV row by row, each one being a CSVRecord object