I have a program that takes in data from the user, calculates the data and is
supposed to display the results in a window. The program compiles and the
user is able to type in data, but when you click the calculate button, the
errors that come up are the following:
java.lang.NumberFormatException: For input string: "Enter Length"
at java.lang.NumberFormatException.forInputString
(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:518)
at Program81$ButtonHandler.actionPerformed(Program81.java:38)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:
17
86)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed
(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed
(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:
258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased
(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
3483
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
The code that the error is referencing is:
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
rectangle r = new rectangle();
r.setWidth(Integer.parseInt(dataWidth.getText()));
r.setLength(Integer.parseInt(enterLength.getText()));
calculateArea.setText("The area of your rectangle is " + r.calcArea());
calculatePerim.setText("The perimeter of your rectangle is " + r.
calcPerimeter());
}
}//end of action handler
What is the cause of this and how do I remedy it? I have no experience with
IO. thank you so much for your help.

Signature
"The man who does not read good books has no advantage over the man who
cannot read them."
fuzzybr80 - 31 Mar 2006 03:27 GMT
Integer.parseInt returns this NumberFormatException error if the input
is not numeric.
>From the error it appears that enterLength.getText() returns the string
"Enter Length" which means the user has not entered a proper number.
You can either check if the string is numeric first, or enclose it in a
try{} catch{} block with a handler to catch NumberFormatExceptions.