Writing a method to read in values from a CSV file. I haven't had much experience with BufferedReader. 4 columns and 50 rows in the test file I'm using. Its supposed to read in and assign to 4 variables(String category, String name, int quantity, BigDecimal price) and with the intent to pass these to a constructor later once I get the reading figured out. I printed the values to see where I'm at, and they are not reading in as expected(see below). I'm looking to read in the two strings, then the integer and lastly the bigdecimal. Its currently chopping some characters and seemingly skipping others, and not printing the new line as expected.
Can anyone help me understand what's going on? Thanks.
Also, why are the -1 and null values printing at the bottom? Am I not correctly closing the file after the 50th row?
public static void readFromFile(){
BufferedReader fileReader = null;
String csvFile = "/Users/Acer/Documents/CS1400/Assignments/InventoryApp/inventory.csv";
int fileSize = 50; //input.nextInt;
try {
fileReader = new BufferedReader(new FileReader(csvFile));
for (int i = 0; i < fileSize; i++){
String category = fileReader.readLine();
String name = fileReader.readLine();
int quantity = fileReader.read();
BigDecimal price = new BigDecimal(fileReader.read());
System.out.println(category + " " + name + " " + quantity + " " + price);
}
}
catch (IOException ioe) {
System.out.println(ioe.getMessage());
} finally {
try {
fileReader.close();
}
catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
Current Output:
Golf,Bag,7,49.99 Baseball,Baseball,4,12.99 72 111
ckey,Puck,8,11.99 Basketball,Basketball,2,15 70 111
otball,Football,7,13.95 Soccer,Ball,6,20.99 71 111
lf,Putter,2,65.5 Baseball,Catchers Mitt,9,49.99 72 111
ckey,Stick,2,27.99 Basketball,Shorts,4,20 70 111
otball,Shoulder Pads,8,34.99 Soccer,Goal,2,44.99 71 111
lf,Tees,4,2.99 Baseball,Cleats,8,50 72 111
ckey,Skates,3,69.99 Basketball,Hoop,5,99.99 70 111
otball,Cleats,2,67.99 Soccer,Shin Guards,4,35.95 71 111
lf,Balls,8,20 Baseball,Pants,5,34.98 72 111
ckey,Gear Bag,3,30 Basketball,Jersey,5,24.99 70 111
otball,Jersey,3,24.99 Soccer,Cleats,6,56.99 71 111
lf,Driver,8,99.99 Baseball,Bat Bag,9,40 72 111
ckey,Goal,9,30 Basketball,Socks,5,20 70 111
otball,Cup,9,12.95 Soccer,Socks,5,12 71 111
lf,3 Wood,2,80.99 Baseball,Socks,3,12.99 72 111
ckey,Neck Guard,2,1.99 Basketball,Catchers Mask,2,32.95 70 111
otball,Cup,7,12 Soccer,Cones,9,11.99 71 111
lf,Irons,3,99.99 Baseball,Sunglasses,6,50 72 111
ckey,Helmet,5,23 Basketball,Shoes,3,74.95 70 111
otball,Helmet,1,13.99 Soccer,Goalie Gloves,8,14.99 71 111
lf,Wedge,2,50 Baseball,Batting Helmet,5,27.99 72 111
ckey,Cup,8,20 Basketball,Sleeves,4,13 70 111
otball,Gloves,6,20.95 Soccer,Jersey,7,30.99 71 111
lf,Cart,5,66.99 Baseball,Bat,6,50.99 -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1
null null -1 -1