0
  1. I am reading csv file by using java program (done successfully)

  2. I want to extract all the data in 2D array (partially done successfully)

Issue I am facing is , when some words having space (eg. "Los Angeles", "New Delhi") then its showing error -

My employee.CSV file is like-

Name,Age,City,Country,Salary

John,25,Los Angeles,US,30000

David,28,Paris,France,45000

Lee-Anne,23,Tokyo,Japan,20000

Hemal,24,New Delhi,India,40000

If there is no space eg. LosAngeles , NewDelhi , then its working fine for me, but i need data with space only.(i wanna use filled array in selenium)

Code i have written is below -

package java2package;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Datadriven {
public static void main(String[] args) {
String[][] myArray = new String[4][5];

    String[] tempArray=new String[5];
    int Rowc=0;
    String fileName="employee.csv";
    File file=new File(fileName);
try 
{

    Scanner content=new Scanner(file);
    content.next();
    while(content.hasNext())
    {
        String data=content.next();
        tempArray=data.split(",");

        for(int i=0;i<5;i++)
        {   
            myArray[Rowc][i]=tempArray[i];

        }

        Rowc++;
    }
    content.close();
    for(int i=0;i<4;i++){
    for(int j=0;j<5;j++){
    System.out.print(myArray[i][j]+" ");
    }
    System.out.print("\n");
    }

} catch (FileNotFoundException e)
{
    System.out.println(e);
}


    }

}

I am using Eclipse, error i am getting is - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at java2package.Datadriven.main(Datadriven.java:31)

Thalesh
  • 1
  • 2
  • And the error is.....? – redFIVE Jun 11 '14 at 22:40
  • Refer to this [question](http://stackoverflow.com/questions/14274259/java-read-csv-with-scanner) – Phil Jun 11 '14 at 22:51
  • I am using Eclipse, error i am getting is - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at java2package.Datadriven.main(Datadriven.java:31) – Thalesh Jun 11 '14 at 22:54
  • Friendly point of procedure. It is best to add the error and stack trace to the actual question itself instead of in the comment section. – hooknc Jun 11 '14 at 23:09
  • @Thalesh are you saying you want all the data from the csv file into a 2D array, including the data with spaces? – Levenal Jun 12 '14 at 00:47
  • @hooknc- Yep, i hv put the error in original question – Thalesh Jun 12 '14 at 04:47
  • @Levenal - yeah, i needed the data with space where its applicable . for eg. "Los Angeles" , i need it as it as "Los Angeles" – Thalesh Jun 12 '14 at 04:50

4 Answers4

0

By using next() you're only reading until the next whitespace character(s). Since you're hardcoding the array length, you get an ArrayIndexOutOfBoundsException because you end up with less elements than you expect.

Using hasNextLine() and nextLine() should yield the results you're looking for. But it would probably be a good idea to check your bounds anyway, since you're working with external data.

shmosel
  • 42,915
  • 5
  • 56
  • 120
  • Exactly , i was looking for such kind of explanations, which can guide me where i m making mistakes, I m new to java, but I tried to replace my code lines with yours while(content.hasNext()) >>> while(content.hasNextLine()) ###### String data=content.nextLine() But unfortunately its not working for me, even i'll say its not working when there is no space available in Employee.csv file (means doesn't work if if put in csv file "LosAngeles", "NewDelhi" at the place of "Los Angeles" , "New Delhi" respectively – Thalesh Jun 12 '14 at 05:03
  • What problem are you having now? – shmosel Jun 12 '14 at 05:43
  • error i am getting with your amended code is - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at java2package.Datadriven.main(Datadriven.java:31) – Thalesh Jun 12 '14 at 19:32
  • What value is `Rowc` when you get the exception? Maybe you have an extra blank line somewhere in the file. Like I mentioned before, if you're not absolutely sure of the format of your file, it's not a good idea to hard code your indexes. – shmosel Jun 13 '14 at 02:54
0

Instead of a scanner you could try using an IO Buffer to read in your data. My example below uses a MappedByteBuffer to go through the csv file and retrieve the data into the arrays.

public class SO {
public static void main(String[] args) {
    String[][] myArray = new String[4][5]; //Your array
    Path file = Paths.get(System.getProperty("user.home")).resolve("Desktop").resolve("tester.csv"); //Path to file
    try {
        RandomAccessFile raf = new RandomAccessFile(file.toString(), "rw"); //Get file
        MappedByteBuffer mbuff = raf.getChannel().map(MapMode.READ_WRITE, 0L, Files.size(file)); //Create mapped buffer to file for bytes transfer

        mbuff.position(28); //  //Name,Age,City,Country,Salary = 28 bytes, so we will position buffer after start headings
        for(int i = 0; i < 4; i++){//For each array
            for(int j = 0; j < 5; j++){//For each element
                myArray[i][j] = getSegment(mbuff); //Get the next 'segment' which should be desired value
                System.out.print("\"" + myArray[i][j] + "\"" + " ");//Print array as proof of what it contains
            }
            System.out.println();
        }

    raf.close();
    } catch (IOException io) {
        io.printStackTrace();
    }
}

public static String getSegment(MappedByteBuffer mb){
    StringBuilder sb = new StringBuilder();//StringBuilder to hold segment
    while(mb.hasRemaining()){
        char c = (char)mb.get();//Each individual char
        if((c == '\n' || c == '\r') && sb.length() == 0){//Ignore newline unless their is value is Stringbuilder
            continue;
        } else if((c == '\n' || c == '\r')){//Newline and something is in the Stringbuilder
                break;//Break loop to return the 'segment'
        } else if(c != ','){ //Not a comma
            sb.append(c); //Append
        } else if(mb.position() == mb.capacity()){ //End of file
            break; //Break loop
        } else {
            break; //Break
        }
    }
    return sb.toString();//Return segment
}
}

Good luck!

Levenal
  • 3,518
  • 2
  • 21
  • 27
0

Better yet, use a csv library, like opencsv, example follows:

CSV csv = CSV
    .separator(',')  // delimiter of fields
    .quote('"')      // quote character
    .create();       // new instance is immutable
csv.read("employee.CSV", new CSVReadProc() {
    public void procRow(int rowIndex, String... values) {
        System.out.println(rowIndex + ": " + Arrays.asList(values));
    }
});

Feel free to comment if you have further questions...

hd1
  • 30,506
  • 4
  • 69
  • 81
0

Here is the easiest template free from defects and it also takes care if your CSV has the first row as header. Good thing is it, doesn't fail if you swap the headers.

Eventually this will return an array of Model. If you wish you can convert into a 2D array of strings. :)

See if this helps:

public class ReadCSV {

public static final String NAME = "Name";
public static final String AGE = "Age";
public static final String CITY = "City";
public static final String COUNTRY = "Country";
public static final String SALARY = "Salary";
public static String[] headers;

public static Model[] readData(String fileName) {
    BufferedReader reader = null;
    String line = null;
    List<Model> models = new ArrayList<Model>();
    String[] split = null;
    int index = 0;
    try {
        reader = new BufferedReader(new FileReader(fileName));
        while ((line = reader.readLine()) != null) {
            split = line.split(",");
            if (split != null && split.length > 0) {
                if (index > 0)
                    models.add(getModel(split));
                else {
                    headers = new String[split.length];
                    System.arraycopy(split, 0, headers, 0, split.length);
                }
            }
            index++;
        }

    }
    catch (FileNotFoundException e) {
    }
    catch (Exception e) {
    }
    finally {
        try {
            reader.close();
        }
        catch (Exception e) {
        }
    }

    return models.toArray(new Model[0]);

}

private static Model getModel(String[] split) {
    Model model = new Model();

    for (int i = 0; i < headers.length; i++) {
        try {
            if (headers[i] != null && headers[i].equalsIgnoreCase(NAME)) {
                model.setName(split[i]);

            }
            else if (headers[i] != null && headers[i].equalsIgnoreCase(COUNTRY)) {
                model.setCountry(split[i]);

            }
            else if (headers[i] != null && headers[i].equalsIgnoreCase(CITY)) {
                model.setCity(split[i]);

            }
            else if (headers[i] != null && headers[i].equalsIgnoreCase(AGE)) {
                model.setAge(convertInt(split[i]));

            }
            else if (headers[i] != null && headers[i].equalsIgnoreCase(SALARY)) {
                model.setSalary(convertDouble(split[i]));

            }
            else {

            }
        }

        catch (Exception e) {
        }
    }

    return model;
}

private static double convertDouble(String s) {
    double value = 0.0;
    try {
        value = Double.parseDouble(s);
    }
    catch (Exception e) {
    }
    return value;
}

private static int convertInt(String s) {
    int value = 0;
    try {
        value = Integer.parseInt(s);
    }
    catch (Exception e) {
    }
    return value;
}

}

class Model {
private String name;
private int age;
private String city;
private String country;
private double salary;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}
}
dharam
  • 7,306
  • 15
  • 59
  • 87