Welcome to Dream.In.Code
Become a Java Expert!

Join 149,515 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!




loading a saved file

 
Reply to this topicStart new topic

loading a saved file, getting a number format error reading a string from a saved file

dgilmore286
30 Jun, 2007 - 04:07 PM
Post #1

D.I.C Head
**

Joined: 9 May, 2007
Posts: 69


My Contributions
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
User is offlineProfile CardPM
+Quote Post

keems21
RE: Loading A Saved File
30 Jun, 2007 - 08:58 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
A NumberFormatException should only be thrown when you're trying to convert a String into a number such as an int, double, etc., however, I don't see where you're doing that in relation to your rating (although I haven't really looked all that hard).

My only guess would be that your variables aren't lining up correctly. To check this, try printing all of them out after you read them from the file to make sure that everything is holding what you expect it to be.
User is offlineProfile CardPM
+Quote Post

dgilmore286
RE: Loading A Saved File
1 Jul, 2007 - 08:42 AM
Post #3

D.I.C Head
**

Joined: 9 May, 2007
Posts: 69


My Contributions
Thank you, I did figure it out. It was a really lame mistake, I had an extra comma in my string and it was causing kanipshits in my file when read

Thank you so much!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month