I am reading csv file by using java program (done successfully)
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)