It seems to be very easy problem to solve but I've stuck on it. Anybody can help me and explain where I do a mistake ? I read a csv file where I have:
Time,Activ,Switch,A,B,C,D,E
5:58,on,,,,,,
6:00,on 123,,,ab cd,,,
6:02,on - off,1->2,6,ab cd,1,,
6:04,on,,,cd ab,1,,
6:06,off,,,,,,
7:22,on / off,,,,,,
7:30,on / of,3->4,15,,,,
7:34,home on,,,ab cd,,,
I would like to separate data in a row only by the commas and save into arrays that will look like (first row omitted):
1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on 123", "", "", "ab cd", "", "", ""]
3---> ["6:02", "on - off", "1->2", "6", "ab cd", "1", "", ""]
6---> ["7:22", "on / off", "3->4", "15", "", "", "", ""]
and so on... but I receive arrays with splitted string also by space and other characters
1---> ["5:58","on", "", "", "", "", "", ""]
2---> ["6:00", "on", "123", "", "", "ab", "cd", "", "", ""]
3---> ["6:02", "on", "-", "off", "1->2", "6", "ab", "cd", "1", "", ""]
6---> ["7:22", "on", "/", "off", "3->4", "15", "", "", "", ""]
Here is my code:
public class Test {
public static void main(String []args) {
String fileName = "TestData.csv";
File file = new File(fileName);
try{
Scanner inputStream = new Scanner(file);
String firstLine = inputStream.next();
while(inputStream.hasNext()){
String dataRow = inputStream.next();
String[] valuesInDataRow = dataRow.split(",",-1);
for(int i=0;i<valuesInDataRow.length;i++){
System.out.println("---> " + valuesInDataRow[i]);
}
}
inputStream.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
I would be very grateful for any explanations :)