I keep getting this error when I'm trying to read in a file and add the data to my objects. Can someone please help me? Here is the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "N64"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at FinalProjectTests.main(FinalProjectTests.java:34)
Here are my objects. Also at the bottom I have setters and getters below the code as well as a toString()
method generated by Eclipse:
Here is my code that is trying to read in a file and add the data to my objects:
public class FinalProjectTests {
//create an arraylist so the objects can be used through them
public static ArrayList<FinalProjectRecord> record = new ArrayList<FinalProjectRecord>();
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(new File("Video Games Sales.csv"));
} catch (FileNotFoundException e) {
System.out.println("file not found");
e.printStackTrace();
}//end try
//skips the title columns in our data file
input.nextLine();
while(input.hasNext()) {
String [] columns = input.nextLine().split(",");
record.add(new FinalProjectRecord(Integer.parseInt(columns[0]),
columns[1],
columns[2],
Integer.parseInt(columns[3]),
columns[4],
columns[5],
Double.parseDouble(columns[6]),
Double.parseDouble(columns[7]),
Double.parseDouble(columns[8]),
Double.parseDouble(columns[9]),
Double.parseDouble(columns[10]),
Double.parseDouble(columns[11])));
}//end while
for(FinalProjectRecord r: record)
System.out.println(r);
}//end main
}//end class
Here is a snippet of the data I am trying to read in:
Edit: I got the data from data.world.
CodePudding user response:
Exception in thread "main" java.lang.NumberFormatException: For input string: "N64" Exception is thrown because of "N64".(Integer.ParseInt or Double.ParseDouble in your code) The image you have posted as data, appears its is from EXCEL Sheet, if so, please search for "N64" and check whether it is correct in that column?
CodePudding user response:
You should really use a library for parsing csv files, this is an example using commons-csv
Here is my hastily made csv file.
'object','value'
'red', 255
'blue', 100
'green, yellow', 50
This simple program will parse it.
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVFormat;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
public class CsvReading{
public static void main(String[] args) throws Exception{
CSVParser parsing = CSVParser.parse(
Paths.get("junk.csv"),
StandardCharsets.UTF_8,
CSVFormat.DEFAULT
);
System.out.println(parsing.getHeaderNames());
System.out.println(parsing.getRecords());
}
}
That will give an output with no headers, and 4 records.
[]
[CSVRecord [comment='null', recordNumber=1, values=['object', 'value']], CSVRecord [comment='null', recordNumber=2, values=['red', 255]], CSVRecord [comment='null', recordNumber=3, values=['blue', 100]], CSVRecord [comment='null', recordNumber=4, values=['green, yellow', 50]]]
You can see the last record has two elements, 'green, yellow'
and 50. If we were to use String.split(",")
then yellow and green would be incorrectly split. This library is very easy to use, I just downloaded the jar file and run the program with.
java -cp commons-csv-1.9.0.jar CsvReading.java