JAVA - import CSV into ArrayList

I am trying to import a CSV file into an Arraylist using a StringTokenizer :

 public class Test { public static void main(String [] args) { List<ImportedXls> datalist = new ArrayList<ImportedXls>(); try { FileReader fr = new FileReader("c:\\temp.csv"); BufferedReader br = new BufferedReader(fr); String stringRead = br.readLine(); while( stringRead != null ) { StringTokenizer st = new StringTokenizer(stringRead, ","); String docNumber = st.nextToken( ); String note = st.nextToken( ); /** PROBLEM */ String index = st.nextToken( ); /** PROBLEM */ ImportedXls temp = new ImportedXls(docNumber, note, index); datalist.add(temp); // read the next line stringRead = br.readLine(); } br.close( ); } catch(IOException ioe){...} for (ImportedXls item : datalist) { System.out.println(item.getDocNumber()); } } } 

I don’t understand how nextToken works, because if I save the initialization of three variables ( docNumber , note and index ) as nextToken() , it fails:

 Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at _test.Test.main(Test.java:32) 

If I hold only docNumber, it works. could you help me?

+6
java arraylist import csv
source share
4 answers

It seems that some of the lines of your input file have less than three fields separated by commas. You should always check if the tokenizer has more tokens (StringTokenizer.hasMoreTokens) if you are not 100% sure that your input is correct.

CORRECT parsing of CSV files is not such a trivial task. Why not use a library that can do this very well - http://opencsv.sourceforge.net/ ?

+18
source share

It seems that your code falls into a line in which the tokenizer is broken into 1 part instead of 3. Is it possible to have lines with missing data? If so, you need to handle this.

+2
source share

Most likely, your input file does not contain another element, limited to at least one line. Please show us your input - if possible, a string that does not work.

However, you do not need to use StringTokenizer . Using String#split() might be simpler:

 ... while( stringRead != null ) { String[] elements = stringRead.split(","); if(elements.length < 3) { throw new RuntimeException("line too short"); //handle missing entries } String docNumber = elements[0]; String note = elements[1]; String index = elements[2]; ImportedXls temp = new ImportedXls(docNumber, note, index); datalist.add(temp); // read the next line stringRead = br.readLine(); } ... 
+2
source share

You should be able to check your tokens using the hasMoreTokens () method. If this returns false, it is possible that the read line does not contain anything (i.e. an empty line).

It would be better to use the String.split () method - if I'm not mistaken, we planned to abandon the StringTokenizer class.

+2
source share

All Articles