Reputation: 171
I am writing a program that reads from a csv file and puts the values into an array of objects. So I set up an object class that has 5 attributes . What I can't seem to get is how to put the tokenized variables into my array of objects. I did a system.out.printf that uses the getters from my object class to print out the object instances in each index but I get a null pointer. So my guess is that the tokens aren't being stored in my array of objects so how can this be done? This is what I have so far.
EDIT: This is one of the lines on my CSV file 12345,Left-Handed Bacon Stretcher,125.95,PGH,2
EDIT: This is the stacktrace error Im getting for those asking java.lang.NumberFormatException: For input string: " 5" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Prog6.main(Prog6.java:97)
Part[] part = new Part[20];
String partNumber;
String description;
double price = 0.0;
String warehouseID;
int quantity = 0;
try
{
inFile = new Scanner( new File( "parts.txt" ) );
}
catch( FileNotFoundException e )
{
System.out.println( "Error: File parts.txt not found" );
}
// read file
try
{
while ( inFile.hasNext() )
{
String record = inFile.nextLine();
String[] tokens = record.split(",[ ]*");
partNumber = tokens[0];
description = tokens[1] ;
price = Double.parseDouble( tokens[2] );
warehouseID = tokens[3] ;
quantity = Integer.parseInt( tokens[4] );
part[num] = new Part( partNumber, description, price, warehouseID, quantity );
num++;
}
}
catch( Exception e )
{
System.out.println( "File error " + e.getMessage() );
}
// close file
inFile.close();
Upvotes: 0
Views: 2150
Reputation: 5092
Try this and see what happen
try
{
int row=1;
while ( inFile.hasNext() )
{
String record = inFile.nextLine();
String[] tokens = record.split(",");
if(tokens.length==5){
partNumber = tokens[0];
description = tokens[1] ;
price = Double.parseDouble( tokens[2].trim());
warehouseID = tokens[3] ;
quantity = Integer.parseInt( tokens[4].trim());
part[num] = new Part( partNumber, description, price, warehouseID, quantity );
num++;
}
else{
System.out.println("Row "+row+" skipped (record not complete)");
}
row++;
}
}
Also, you need to check whether tokens[2]
and tokens[4]
is parseable. in case its value is empty string(""), space or not a number.
Upvotes: 1
Reputation: 4466
First check if your program is going inside while loop, you can either debug or add a print statement
If its going inside while then check the lenght of tokens
Also is it posisble that your file has less than 20 records ?
Also share your exception statck trace and code you using to print each object
Edit:
Instean of System.out.println( "File error " + e.getMessage() );
in catch block write e.printStackTrace()
use this instead:
Integer.parseInt( tokens[4].trim() );
Upvotes: 0
Reputation: 2833
The problem is that one of the lines in your .csv file look like so:
blah,blah,blah,blah, 5
Notice the space there, that is what is causing the NumberFormatException. Remove this space from your .csv , try (as Rafa El suggests) trimming the String before parsing to an Integer, or try splitting on the following regex: " *, *"
.
Upvotes: 0