Although using the Apache CSV library as mentioned by @Minjun.Y is perfectly fine, I try to provide a solution that is closer to your code and maybe easier for you to follow:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class CsvParser {
public static void main(String[] args) {
String fileName= "read_ex.csv";
File file= new File(fileName);
// this gives you a 2-dimensional array of strings
List<List<String>> lines = new ArrayList<>();
Scanner inputStream;
try{
inputStream = new Scanner(file);
while(inputStream.hasNext()){
String line= inputStream.next();
String[] values = line.split(",");
// this adds the currently parsed line to the 2-dimensional string array
lines.add(Arrays.asList(values));
}
inputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
// the following code lets you iterate through the 2-dimensional array
int lineNo = 1;
for(List<String> line: lines) {
int columnNo = 1;
for (String value: line) {
System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);
columnNo++;
}
lineNo++;
}
}
}
Let's go through it step-by-step:
I added 3 more imports: ArrayList
, Arrays
and List
- you will see very soon for what they are good. They are all taken from the java.util
library, a standard library that is available with every JDK.
Every class name in Java starts with a capital letter (by convention - it will build with a small letter as well, but you should get used to this convention) - I "fixed" this in your code.
I added a 2-dimensional array List<List<String>> lines = new ArrayList<>()
. This might look a little confusing at first, but what it means is that we create a variable lines
that holds the results of our parsing. The List<String>
syntax means, that we have a generic type List
that has a type parameter String
- in other words: a list of strings. The whole List<List<String>>
means we have a list of lists of strings, a 2-dimensional string array.
With the lines.add(Arrays.asList(values))
in your while
loop, we can add the lines that you parsed to this 2-dimensional array. Arrays.asList(values)
transforms the String[]
array into a List
as we need it to be compatible to our List<List<...>>
type. This allows your lines to have variable length.
The last lines I added simply print the content of 2-dimensional array and should give you a good example for how to access the values in this array. If you need further help for this construct, check the foreach loop documentation.
Given this as an input file (read_ex.csv
):
value_1-1,value_1-2,value_1-3,value_1-4
value_2-1,value_2-2,value_2-3,value_2-4
value_3-1,value_3-2,value_3-3,value_3-4
value_4-1,value_4-2,value_4-3,value_4-4
value_5-1,value_5-2,value_5-3,value_5-4
The program will print the following output:
Line 1 Column 1: value_1-1
Line 1 Column 2: value_1-2
Line 1 Column 3: value_1-3
Line 1 Column 4: value_1-4
Line 2 Column 1: value_2-1
Line 2 Column 2: value_2-2
Line 2 Column 3: value_2-3
Line 2 Column 4: value_2-4
Line 3 Column 1: value_3-1
Line 3 Column 2: value_3-2
Line 3 Column 3: value_3-3
Line 3 Column 4: value_3-4
Line 4 Column 1: value_4-1
Line 4 Column 2: value_4-2
Line 4 Column 3: value_4-3
Line 4 Column 4: value_4-4
Line 5 Column 1: value_5-1
Line 5 Column 2: value_5-2
Line 5 Column 3: value_5-3
Line 5 Column 4: value_5-4
Hope this helps :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…