Parsing data from CSV to array in Java

I am trying to import a CSV file into an array that I can use in a Java program. The CSV file successfully imported itself, and the output appears on the terminal, but it throws an error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at CompareCSV.main(CompareCSV.java:19) 

in the end. In addition, when I try to call elements in an array, it also shows the same error. My code is below:

 import java.io.*; import java.util.*; public class CompareCSV { public static void main(String[] args) { String fileName = "sampledata1.csv"; try { BufferedReader br = new BufferedReader( new FileReader(fileName)); String strLine = null; StringTokenizer st = null; int lineNumber = 0, tokenNumber = 0; while((fileName = br.readLine()) != null) { lineNumber++; String[] result = fileName.split(","); for (int x=0; x<result.length; x++) { System.out.println(result[x]); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 
+6
source share
4 answers

You are much better off using the correct CSV parser than hacking the faulty one yourself: http://opencsv.sourceforge.net/

CSV is not a simple format to think of (yes, a string may contain, which does not separate the two parts of the data).

+6
source

This is the answer to the previous question.

  public class Readline { /** * @param args */ public static void main(String[] args) { String fileName = "C:/Users/karthikrao/Desktop/cvsFile.csv"; ArrayList<Integer> margins = new ArrayList<Integer>(); BufferedReader br; String line, token; int i; try { br = new BufferedReader(new FileReader(fileName)); try { while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ",\""); i = 0; while (st.hasMoreTokens()) { token = st.nextToken(); if (margins.size() <= i) { margins.add((Integer) token.length()); } else { margins.set( i, Math.max(margins.get(i), (Integer) token.length())); } i++; } } br = new BufferedReader(new FileReader(fileName)); while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ",\""); i = 0; while (st.hasMoreTokens()) { token = st.nextToken(); System.out.print(token); for (int j = 0; j < margins.get(i) - token.length(); j++) { System.out.print(" "); } System.out.print("|"); i++; } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } 

}

+2
source

I suggest you not reinvent the wheel when there are so many great libraries. Try uniVocity-parsers with the following snippt code as a link:

 public static void main(String[] args) throws FileNotFoundException { /** * --------------------------------------- * Read CSV rows into 2-dimensional array * --------------------------------------- */ // 1st, creates a CSV parser with the configs CsvParser parser = new CsvParser(new CsvParserSettings()); // 2nd, parses all rows from the CSV file into a 2-dimensional array List<String[]> resolvedData = parser.parseAll(new FileReader("/examples/example.csv")); // 3rd, process the 2-dimensional array with business logic // ...... } 

As you can see, only 2 rows are required to complete the task of parsing csv data into an array. In addition, the library provides a complete list of features for analyzing CSV data with excellent performance.

+2
source

It seems your assumption that a line in a file always has three columns does not match all the lines. Replace the for loop statement with the following line to eliminate the exception and see why this happened:

 for (int x=0; x<result.length; x++) 
0
source

Source: https://habr.com/ru/post/891680/


All Articles