Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read NumberFormatException error message?

[NOTE] Add to the answer below for other good tips for reading the stack trace

I am getting an error in a program that I am making and I don't know how to fix it.

The user has to choose options from messageboxes, and I have to use the users input to calculate the tuition they will have and their discount.

If I run it!

 run: Exception in thread "main" java.lang.NumberFormatException: For input string: ""
                at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                at java.lang.Integer.parseInt(Integer.java:468)
                at java.lang.Integer.parseInt(Integer.java:497)
                at cabrera.Main.main(Main.java:26)
        Java Result: 1
        BUILD SUCCESSFUL (total time: 6 seconds)

Code

package cabrera;

/**
 *
 * @author Owner
 */
import javax.swing.JOptionPane;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String tuition1 = "", scholar1 = "";
        int tuition = 0, scholar = 0;
        double discount = 0.0;

        JOptionPane.showInputDialog("Input Your Tuition" + tuition1);
        JOptionPane.showInputDialog("Choose A Scholar Discount:" + "\n• 1 = 80% " + "\n• 2 = 50%" + "\n• 3 = 25%" + "\n• 4 = No Discount !" + scholar1);

        tuition = Integer.parseInt(tuition1);
        scholar = Integer.parseInt(scholar1);

        JOptionPane.showMessageDialog(null, "Your Total Tuition is: " + tuition);

        // If-elseif-else statements based on scholar variable

    }
}

How do I fix this error message?

More Info

I recently came across a question that was badly executed. Bad title, bad code, bad question (actually not question, but it could be assumed after some thought). It was from a new SO user.

I wanted to help out and looked at the code for a while. So I edited the post; fixed the code layout, simplified the code, ask the question the OP wanted to ask, etc.... That question is now above; however, the question was put on hold, and I read that questions can take a long time to get off of On Hold. You need something like 5 votes to reopen after it is put on hold. Something I don't see that will happen given that it has been a while since the question was first asked.

I had the answer complete and don't want to waste a good answer

like image 626
Christopher Rucinski Avatar asked Nov 30 '22 01:11

Christopher Rucinski


1 Answers

OK, lets look at how to find the issue first!

(3) run: Exception in thread "main" java.lang.NumberFormatException: For input string: ""
               at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
               at java.lang.Integer.parseInt(Integer.java:468)
(2)            at java.lang.Integer.parseInt(Integer.java:497)
(1)            at cabrera.Main.main(Main.java:26)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)

Look at...

  • (1), here you can see in Main.java you have an issue on line 26
  • (2), here you can see more information on the issue. In fact you know you have an issue with Integer.parseInt.

    • You Don't have to worry about looking at this code, because you don't have a class called Integer with method parseInt in the package java.lang
    • You just know there is an issue here, and it deals with Integer.parseInt which you do call in your Main.java code!

    • So we located a possible area to check for the error!

      tuition = Integer.parseInt(tuition1);    
      scholar = Integer.parseInt(scholar1);
      
  • (3), here we can see that a NumberFormatException was throw because you gave it (the Integer.parseInt that is) an input of "" (an empty string)


So How do you fix it?

  1. First, you have to look at the input of Integer.parseInt

    • You can see you pass in tuition1 and scholar1

  2. Look to see where those 2 variable are changed / created.

    • You can see the last thing you did was create those two variables and assigned them the empty string ("")
    • There is the problem!!

  3. So you need to assign them a value from the user. That will be done via the showInputDialog(...).

    • the showInputDialog(...) returns a value that you can use! See here!
    • All you have to do is...

      tuition1 = JOptionPane.showInputDialog("Input Your Tuition");     
      scholar1 = JOptionPane.showInputDialog("Choose A Scholar Discount: 1... 2... 3... 4...")
      
    • Take note that tuition1 and scholar1 should NOT be placed inside of the dialog box when you want to assign them values from the user. You need to have the user input some text into the dialog box, and that value will be returned when you press the OK button. So you have to assign that return value to tuition1 and scholar1

like image 65
Christopher Rucinski Avatar answered Dec 04 '22 10:12

Christopher Rucinski