I am trying to read a comma-separated value file but I can't seem to get my code to work. Any clue where my code is incorrect?
Notes: Trying to read only the Price column and make sure the values are not NULL, N/A, less than 80 and greater than 130.
SpreadSheet:
boolean flag = true;
File inputF = new File("data.csv")
InputStream getStream = new FileInputStream(inputF);
try{
if(getStream.read() != -1){
BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
String line;
while((line = reader.readLine()) != null){
String[] csvs = line.split(",");
for (String str : csvs){
if (str != null && (str.equals("NA") || str.length() == 0)) {
System.out.println(str);
flag = false;
break;
}
}
if (!flag){
break;
}else{
String Price = csvs[1];
price = price.trim();
Integer priceValue = new Integer(Price); //exception occurs here
if (priceValue != null && (priceValue < 80 || priceValue > 130)){
flag = true;
}
}
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(flag);
return flag;
Error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "PRICE"
Expected Output: (Based on spreadsheet provided)
True