2 Replies Latest reply: Apr 3, 2007 2:23 PM by 807606 RSS

    NumberFormatException

    807606
      I am not sure if this is in the correct forum. If not, let me know and I will relocate.

      This method in a class EntireMeal. When the user chooses to save the meal (consisting of multiple items) then a new instance is created. In order to differentiate between meals on the same day, each meal for each day is given it's own unique(for that day) ID#. This method sets up that variable for the instance of EntireMeal.
       /*
            Method will work through each meal in meal.txt
            in order to find what the ID for this meal should be:
            1. Turn the date into a GregorianCalendar date.
            2. Compare that date to GregorianCalendar eaten
            --which is the date the meal-in-question is set for.
            3. If they are on the same day--increment the ID counter.
            4. Check other meal entries.
            5. Return the incremented integer, 
            --which is the next sequential ID # for that day.
           */
          private int findID()
          {
           String what = new String();
           int countID = 0;
       
           try
               {
                BufferedReader bfr = new BufferedReader
                    (new FileReader("meals.txt"));
                
                while((what = bfr.readLine()) != null)
                    {
                     System.out.println("Date: "+what);
                     /*
                       read date from file, turn it into comparable date
                       for our "eaten" variable
                     */
                     Date date = new Date();
                     SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd yyyy");
                     try{date = formatter.parse(what);formatter.format(date);}
                     catch(ParseException exi){}
                     GregorianCalendar compare = new GregorianCalendar();
                     compare.setTime(date);
                
                     if(compare.equals(eaten))
                         countID++;
       
                     System.out.println("The ID: "+bfr.readLine());//ID
                     
                     /*
                       check how many items constitute that meal
                       and move down 3 lines for each item
                      */
                     int howManyItems = Integer.parseInt(bfr.readLine());
                     System.out.println("How Many: "+howManyItems);
                     for(int x = 0; x < howManyItems; x++)
                         {
                          System.out.println("1: "+bfr.readLine());
                          System.out.println("2: "+bfr.readLine());
                          System.out.println("3: "+bfr.readLine());
                         }
                    }
                bfr.close();
               }catch(IOException ex){}
           
           return countID;
          }
      The file looks like this:

      Mon Apr 02 2007
      0
      1
      Anchovies
      1
      300

      and is written to with this code:
          /*
            The meals.txt will look like this:
            Date
            MealID
            number of items
            Name of item 1
            Calories per serving of that item
            how many servings
            Name of item 2
            etc etc
           */
          public void saveMeal()
          {
           try
               {
                BufferedWriter nbw = new BufferedWriter
                    (new FileWriter("meals.txt",true));
       
                //write the date
                Date date = eaten.getTime();
                SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd yyyy");
                String d = formatter.format(date);
                nbw.write(d,0,d.length());
                nbw.newLine();
                //write the ID
                String i = Integer.toString(mealID);
                nbw.write(i,0,i.length());
                nbw.newLine();
                //write the number of items
                String f = Integer.toString(nameOfItemVec.size());
                nbw.write(f,0,f.length());
                nbw.newLine();
                //write those items!
                String n = new String();
                String m = new String();
                String o = new String();
                for(int x = 0; x < nameOfItemVec.size(); x++)
                    {
                     n = (nameOfItemVec.get(x)).toString();
                     nbw.write(n,0,n.length());
                     nbw.newLine();
                     m = (caloriesPerVec.get(x)).toString();
                     nbw.write(m,0,m.length());
                     nbw.newLine();
                     o = (servingsVec.get(x)).toString();
                     nbw.write(o,0,o.length());
                     nbw.newLine(); 
                    }
                nbw.close();
               }
           catch(IOException ex){}
      The error I am getting is a NumberFormatException: null at line 132 which is
      int howManyItems = Integer.parseInt(bfr.readLine());
      The output for those debugging output statements in findID() looks like this:
      Date: Mon Apr 02 2007
      The ID: 0
      How Many: 1
      1: Anchovies
      2:
      3: 1
      Date: 300
      The ID: null

      Obviously, the date should be null, terminating the while loop. But that second bfr.readLine() in the for loop is being ignored. In fact, interestingly enough, if I copied and paste from the .txt file it looked like this:

      Mon Apr 02 2007
      0
      1
      Anchovies

      1
      300

      However, when I open it there is no gap between Anchovies and 1. And my BufferedWriter seems to get the job done.

      I have been messing with this for way too long and I need some outside input. Does anyone have any clue what may be causing this? It is beyond me....
      Thanks.
      Jeremy
        • 1. Re: NumberFormatException
          807606
          Hmm, looks like the "Anchovies" line ends with a strange code; you should have a look at it using a hex editor.
          • 2. Re: NumberFormatException
            807606
            Thanks. I took your advice. Here is what I came up with:
            (I indented on my own for easy reading, also the data is a bit different: "Chicken" instead of "Anchovies" for instance).

            54 75 65 20 41 70 72 20 30 33 20 32 30 30 37
            0d 0a
            30
            0d 0a
            31
            0d 0a
            43 68 69 63 6b 65 6e
            09 0d 0d 0a
            32 30 30
            0d 0a
            31
            0d 0a

            Character version:
            (once again, without my editing, there is an empty line between Chicken and 200, but opening the file in notepad doesn't display this, only when I copy-paste)

            Tue Apr 03 2007
            0
            1
            Chicken     
            200
            1

            In the hexadecimal version the "0d 0a" is carriage return and new line, which is what all the lines are doing except after "Chicken" which then goes "09 0d 0d 0a." That is a horizontal tab, new line, carriage return, new line.

            I don't see it anywhere in my write-to-file code:
                 try
                     {
                      BufferedWriter nbw = new BufferedWriter
                          (new FileWriter("meals.txt",true));
            
                      //write the date
                      Date date = eaten.getTime();
                      SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd yyyy");
                      String d = formatter.format(date);
                      nbw.write(d,0,d.length());
                      nbw.newLine();
            
                      //write the ID
                      String i = Integer.toString(mealID);
                      nbw.write(i,0,i.length());
                      nbw.newLine();
            
                      //write the number of items
                      String f = Integer.toString(nameOfItemVec.size());
                      nbw.write(f,0,f.length());
                      nbw.newLine();
            
                      //write those items!
                      String n = new String();
                      String m = new String();
                      String o = new String();
                      for(int x = 0; x < nameOfItemVec.size(); x++)
                          {
                           n = (nameOfItemVec.get(x)).toString();
                           nbw.write(n,0,n.length());
                           nbw.newLine();
                           m = (caloriesPerVec.get(x)).toString();
                           nbw.write(m,0,m.length());
                           nbw.newLine();
                           o = (servingsVec.get(x)).toString();
                           nbw.write(o,0,o.length());
                           nbw.newLine(); 
                          }
                      nbw.close();
                     }
                 catch(IOException ex){}
            Anyway, I am side-stepping this problem, even though it is eventually crucial. For the life of me, I just don't have an answer. I think the answer is one that is beyond my knowledge.
            Jeremy