Hi all,
Can someone please help me with a couple functions?
I have button in my GUI that saves data to a file and that seems to be operating fine.
I have another button to load the file and I am getting an error regarding number format for a string that represents the movie rating for my objects. I cannot figure out what I am doing wrong.
The actual error is:
java.lang.NumberFormatException: for input string "PG"
It is regarding a string for a movie rating of PG-13...
Any help or ideas will be much appreciated. This is for a class project that is due Sunday, it is now Saturday and I have been killing myself over this for over a week...
Here is the code for the save file button:
CODE
private void saveFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
FileOutputStream fileOut;
PrintStream filePrinter;
try {
// create the directory and the file
File aDirectory = new File("C:\\data");
aDirectory.mkdir();
// create the file
File theFile = new File("C:\\data\\movie_info.dat");
fileOut = new FileOutputStream( theFile );
filePrinter = new PrintStream( fileOut );
// loop through the list of DVDs
for (int i = 0; i < counter; i++) {
Movie d = products[i];
// and print each product's description to the file
// DVD information is comma separated
filePrinter.println(
d.getProdTitle() + ","
+d.getMovieRating() + ","
+d.getProdNumber() + ","
+d.getProdCount() + ","
+d.getProdPrice()
);
}
// close printing to the file
filePrinter.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "There was a problem saving the file:\n" + e);
}
}
And here is the code for the load file button:
CODE
private void loadFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String loadFileName =( "C:\\data\\movie_info.dat");
Movie tempProduct[] = new Movie[maxNumberOfProducts];
counter = 0;
File theFile = new File(loadFileName);
// open the file for reading
BufferedReader bufReader = new BufferedReader(new FileReader(theFile));
String line = null;
//read each line of text file
while((line = bufReader.readLine()) != null) {
// split up the line based on the commas
StringTokenizer st = new StringTokenizer(line,",");
//Assign the text fields to variables
String movieTitle = st.nextToken().trim();
String movieRating = st.nextToken().trim();
String strItemNumber = st.nextToken().trim();
String strQuantity = st.nextToken().trim();
String strPrice = st.nextToken().trim();
//Convert text into int and double.
int item_number = Integer.parseInt( strItemNumber );
int quantity = Integer.parseInt( strQuantity );
strPrice = strPrice.replaceAll("\\$", "");
Double price = Double.parseDouble( strPrice );
// add the new DVD from the file to the new inventory
tempProduct[counter++] = new Movie(movieTitle, movieRating, item_number, quantity, price );
}
//close the file
bufReader.close();
// replace the exisiting inventory with the new one from the file
products = tempProduct;
} catch (Exception loadException) {
JOptionPane.showMessageDialog(null, "There was a problem loading the inventory:\n" + loadException);
}// TODO add your handling code here:
}
This post has been edited by dgilmore286: 30 Jun, 2007 - 04:08 PM