I am trying to read a .csv file which has 6 columns, but some rows in the last column is empty cell.
item1 2 4 125 200
item2 5 8 250 375
item3 2 3 125 200
item4 2 4 200 325 10
item5 1 2 325 400 10
item6 1 2 250 350 10
Result
row 0: item1
row 1: 2.0
row 2: 4.0
row 3: 125.0
row 4: 200.0
new --------: 0
row 0: item2
row 1: 5.0
row 2: 8.0
row 3: 250.0
row 4: 375.0
new --------: 0
row 0: item3
row 1: 2.0
row 2: 3.0
row 3: 125.0
row 4: 200.0
- Java code should get all the rows, parse each value into double.
- The last column if its empty, print a 0 value, otherwise parse the last column value
QUESTION: How to i get empty cell value for rows and set int lastColEmpytyValue = 0;?
while(inputStream.hasNext()) {
String data = inputStream.nextLine(); // gets a while line .next gets next word i.e. ice. .nextLine get word with spaces i.e. ice cream
String[] row = data.split(",", -1); // regex - split line
System.out.println("row 0: " + row[0] );
System.out.println("row 1: " + Double.parseDouble(row[1] ));
System.out.println("row 2: " + Double.parseDouble(row[2]) );
System.out.println("row 3: " + Double.parseDouble(row[3]) );
System.out.println("row 4: " + Double.parseDouble(row[4]) );
if (row[5] == null || row[5].isEmpty() || row[5] == "") {
int row5;
row[5] = "0";
row5 = Integer.parseInt(row[5]);
System.out.println("new --------: " +row5);
} else {
System.out.println("row 5.....: " + row[5]);
}
}