Results 1 to 6 of 6
  1. #1
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default NumberFormatException- shouldn't be happening?

    I've got a little class here that's supposed to calculate a GPA using simple JOptionPane GUI stuff and arrays. I'm having an issue though- as soon as my method takeIn() is finished running, the program throws a NumberFormatException, ostensibly in relation to the ParseDouble in line 68 (the error message is actually giving a line number of 510, which confuses me, but it does mention the ParseDouble so I figure that's the issue). At first I thought this was due to my for-loop in takeIn() not properly converting the letter grades to numeric strings, but after a little debug I've found that it's converting them just fine. Can anyone shed some light on this?

    Java Code:
    public class GPAcalc{
      String[][] inputs;
    
      public GPAcalc(){
        inputs = new String[50][4];
      }
      public int takeIn(){
        int x = 0;
        int continueProgram;
        boolean exit = false;
        JOptionPane.showMessageDialog(null,"Welcome to the GPA Calculator!","Welcome",JOptionPane.INFORMATION_MESSAGE);
        while(!exit){
          inputs[x][0] = JOptionPane.showInputDialog(null,"Please enter the course number: ","Course Number",JOptionPane.QUESTION_MESSAGE);
          inputs[x][1] = JOptionPane.showInputDialog(null,"Please enter the course name: ","Course Name",JOptionPane.QUESTION_MESSAGE);
          inputs[x][2] = JOptionPane.showInputDialog(null,"Please enter your grade: ","Grade Received",JOptionPane.QUESTION_MESSAGE);
          inputs[x][3] = JOptionPane.showInputDialog(null,"Please enter the number of credit hours: ","Credit Hours",JOptionPane.QUESTION_MESSAGE);
          x++;
          continueProgram = JOptionPane.showConfirmDialog(null,"Enter another class?","Enter another class?",JOptionPane.YES_NO_OPTION);
          if(continueProgram == 0)
            continue;
          else if(continueProgram == 1)
            exit = true;
        }
        for(int i = 0; i < (x-1); i++){
          for(int j = 0; j < 4; j++){
            if(inputs[i][j].equals("A") || inputs[i][j].equals("A+"))
              inputs[i][j] = "4.0";
            else if(inputs[i][j].equals("A-"))
              inputs[i][j] = "3.7";
            else if(inputs[i][j].equals("B+"))
              inputs[i][j] = "3.33";
            else if(inputs[i][j].equals("B"))
              inputs[i][j] = "3.0";
            else if(inputs[i][j].equals("B-"))
              inputs[i][j] = "2.7";
            else if(inputs[i][j].equals("C+"))
              inputs[i][j] = "2.3";
            else if(inputs[i][j].equals("C"))
              inputs[i][j] = "2.0";
            else if(inputs[i][j].equals("C-"))
              inputs[i][j] = "1.7";
            else if(inputs[i][j].equals("D+"))
              inputs[i][j] = "1.3";
            else if(inputs[i][j].equals("D"))
              inputs[i][j] = "1.0";
            else if(inputs[i][j].equals("D-"))
              inputs[i][j] = "0.7";
            else if(inputs[i][j].equals("F"))
              inputs[i][j] = "0.0";
          }
        }
        return x;
      }
      public int addCredits(int classes){
        int totCredits = 0;
        for(int i = 0; i < classes; i++){
          totCredits = totCredits + Integer.parseInt(inputs[i][3]);
        }
        return totCredits;
      }
      public void calculate(int credits, int classes){
        int creditsFailed = 0;
        double gradePoints = 0.0;
        double gpa;
        String display1 = "";
        String display2 = "";
        for(int i = 0; i < classes; i++){
          gradePoints = gradePoints + (Double.parseDouble(inputs[i][2]) * Double.parseDouble(inputs[i][3]));
          if(inputs[i][2].equals("F"))
            creditsFailed = creditsFailed + Integer.parseInt(inputs[i][3]);
        }
        gpa = gradePoints/credits;
        for(int i = 0; i < classes; i++){
          display1 = display1 + inputs[i][0] + ": " + inputs[i][2] + "\n";
        }
        JOptionPane.showMessageDialog(null, display1,"Classes Taken",JOptionPane.INFORMATION_MESSAGE);
        display2 = "Credits Taken: " + credits + "\nCredits Earned: " + (credits - creditsFailed) + "\nFinal Grade Point Average: " + gpa;
        JOptionPane.showMessageDialog(null, display2,"Final Report",JOptionPane.INFORMATION_MESSAGE);
      }
      public static void main(String[] args){
        GPAcalc g = new GPAcalc();
        int classesTaken = g.takeIn();
        g.calculate(g.addCredits(classesTaken),classesTaken);
      }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: NumberFormatException- shouldn't be happening?

    Have you printed out the values just before you convert them to ensure they are in the correct format? Also, you should include the error message in future posts.

    And one observation. Inputs[i][2] contains the grade you are converting to a number. So why are you iterating 0 thru 3 in the nested loop above where you make the assignments?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: NumberFormatException- shouldn't be happening?

    When I try to input A for the grade I got the following error:

    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
    	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    	at java.lang.Double.parseDouble(Double.java:540)
    	at GPAcalc.calculate(GPAcalc.java:70)
    	at GPAcalc.main(GPAcalc.java:85)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    It was in the GPAcalc.calculate(GPAcalc.java:70). It means on this line:

    Java Code:
    gradePoints = gradePoints + (Double.parseDouble(inputs[i][2]) * Double.parseDouble(inputs[i][3]));
    It tries to covert A to double. I guest you are accessing the wrong index of the inputs array in that line above.

  4. #4
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: NumberFormatException- shouldn't be happening?

    Here is the error message:
    Java Code:
    java.lang.NumberFormatException: For input string: "A"
    	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    	at java.lang.Double.parseDouble(Double.java:510)
    	at GPAcalc.calculate(GPAcalc.java:83)
    	at GPAcalc.main(GPAcalc.java:98)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
    I already checked to see if the letter strings being entered into the array were being converted into numbers, and they are.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: NumberFormatException- shouldn't be happening?

    Try changing:
    Java Code:
    for(int i = 0; i < (x-1); i++){
    to
    Java Code:
    for(int i = 0; i < x; i++){
    And please answer my question about the inner loop going from 0 to 3. I don't believe it is necessary.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: NumberFormatException- shouldn't be happening?

    That did the trick. Thanks very much. I understand now why the error message was coming up- the last letter grade was not being changed to a number.

    And you're correct, it's not necessary. I realized that after I coded it and was just being lazy about changing it.

Similar Threads

  1. This shouldn't be too hard.
    By MR bruto in forum New To Java
    Replies: 4
    Last Post: 05-10-2013, 05:20 PM
  2. Replies: 2
    Last Post: 12-31-2011, 04:50 AM
  3. I don't know what is happening
    By Fred1 in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 11:21 PM
  4. Final variables shouldn't also be static?
    By emilioJazz in forum Advanced Java
    Replies: 5
    Last Post: 11-29-2010, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •