skip to Main Content

I am trying to get the values out of String[] value; into String lastName;, but I get errors and it says java.lang.ArrayIndexOutOfBoundsException: 2
at arduinojava.OpenFile.openCsv(OpenFile.java:51) (lastName = value[2];)
. Here is my code, but I am not sure if it is going wrong at the split() or declaring the variables or getting the data into another variable.

Also I am calling input.next(); three times for ignoring first row, because otherwise of study of Field of study would also be printed out..

The rows I am trying to share are in a .csv file:

University  Firstname   Lastname    Field of study
Karlsruhe   Jerone          L           Software Engineering 
Amsterdam   Shahin          S           Software Engineering 
Mannheim    Saman           K           Artificial Intelligence
Furtwangen  Omid            K           Technical Computing
Esslingen   Cherelle        P           Technical Computing

Here’s my code:

// Declare Variable
JFileChooser fileChooser = new JFileChooser();
StringBuilder sb = new StringBuilder();
//    StringBuilder data = new StringBuilder();
String data = "";
int rowCounter = 0;
String delimiter = ";";
String[] value;
String lastName = "";

/**
* Opencsv csv (comma-seperated values) reader
*/
public void openCsv() throws Exception {

    if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        // Get file
        File file = fileChooser.getSelectedFile();

        // Create a scanner for the file
        Scanner input = new Scanner(file);

        // Ignore first row
        input.next();
        input.next();
        input.next();

        // Read from input
        while (input.hasNext()) {
            // Gets whole row
            //                data.append(rowCounter + " " + input.nextLine() + "n");
            data = input.nextLine();
            // Split row data
            value = data.split(String.valueOf(delimiter));
            lastName = value[2];
            rowCounter++;
            System.out.println(rowCounter + " " + data + "Lastname: " + lastName);
        }
        input.close();
        } else {
        sb.append("No file was selected");
    }
}

3

Answers


  1. lines are separated by spaces not by semicolon as per your sample. Try in this way to split based on one or more spaces.

    data.split("\s+");
    

    Change the delimiter as shown below:

    String delimiter = "\s+";
    

    EDIT

    The CSV file should be in this format. All the values should be enclosed inside double quotes and there should be a valid separator like comma,space,semicolon etc.

    "University"  "Firstname" "Lastname"     "Field of study"
    "Karlsruhe"   "Jerone"        "L"       "Software Engineering" 
    "Amsterdam"   "Shahin"        "S"       "Software Engineering"
    
    Login or Signup to reply.
  2. Please check if you file is using delimiter as ‘;’ if not add it and try it again, it should work!!

    Login or Signup to reply.
  3. Use OpenCSV Library for read CSV files .Here is a detailed example on read/write CSV files using java by Viral Patel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search