Question

I'm making a calculator, and I'm getting a NumberFormatException.

This is the code for my method:

String[] parts = text.getText().split(" + ", 2);
temporary[0] = Integer.parseInt(parts[0]);
temporary[1] = Integer.parseInt(parts[0]);
answer = temporary[0] + temporary[1];

This is the code for my class:

public int answer = 0;
public int[] temporary = {0, 0};

I'm getting the NFE on this line:

temporary[0] = Integer.parseInt(parts[0]);

Any ideas why?

This is my stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "8 + 9"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at main.Calculator.actionPerformed(Calculator.java:123)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Was it helpful?

Solution

split uses regex so split(" + ") will try to split on two or more continues spaces and since your String probably don't have such spaces it will not be split so parts[0] will hold entire original String. Because of that your code will try to parse something like

Integer.parseInt("123 + 321")` 

which throws NumberFormatException because it is not correct integer this method can parse. Try escaping + in split. You can also make spaces optional.

Try with

String[] parts = text.getText().split("\\s*\\+\\s*", 2);

Also note that you are trying to parse parts[0] twice. Change your

temporary[1] = Integer.parseInt(parts[0]);

to

temporary[1] = Integer.parseInt(parts[1]);

OTHER TIPS

The first argument to split is a regular expression. If you're looking for a plus sign, this string won't do the job, because " + " means look for 1 or more spaces, followed by another space. That is, it looks for 2 or more spaces as a delimiter. So if your input string is "2 + 2", there's no 2-space sequence, so split will return a one-element array with "2 + 2" as the string. This isn't the right format for a number.

To search for a space followed by a plus sign followed by a space:

String[] parts = text.getText().split(" \\+ ", 2);

Then fix it so it doesn't multiply when they ask to add two numbers :) [OK, you did that.]

Note: If you eventually want to let your users do something besides addition, you probably won't be able to use split without a fairly complex regular expression. That's because split won't return the delimiters. That's fine if the only possible delimiter is +, but if you can have other operators, you'll need to know what the operator is, so split won't work. You'll need to use more general regex matching. See this tutorial.

The documentation says why.

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

Throws:

NumberFormatException - if the string does not contain a parsable integer.

As many has pointed out here, you must check first if the string you want to parse actually contains a number or not.

Now, if your idea is to parse an expression for evaluation, the right paths for such a task are

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top