• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to use parseInt()

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where should i equate the Interger.parseInt();? :roll: :roll: :roll:
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jherald,
Can you rephrase your question or make it more detail?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java, we can change any data type to string by adding with a string.
if i have the String
int i=0;
String str=""+i;

now the value of i is 10 and the value of str also i.
we can perform mathematical operation on i and not str eventhouth str is an integer.
so we have to convert the str to an integer format.
for that
int x=Integer.parseInt(str);
then the x contains integer variable.

The solution is
from the string having the integer value, we can get integer using the parseInt funtion.
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx for the reply

when i use this:

int num = Integer.parseInt(text2);

the error generated was:

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

java.lang.NumberFormatException: For input string: " "

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java)

at java.lang.Integer.parseInt(Integer.java:518)

at mypackage.Calculator$16.actionPerformed(Calculator.java:183)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.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(BasicButtonListener.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)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java)

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.Component.dispatchEvent(Component.java)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Process exited with exit code 0.
 
Ramaswamy Narayanan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need extra information about the 'text2' variable.
is it a String variable contains integer data?
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static String text2 = new String();
Calculator.text2 = Calculator.txt.getText();
int num = Integer.parseInt(text2);
 
Yosi Hendarsjah
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the error code we can see that variable text2 contains only a space. So it can't be converted into integer.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jherald Lacambra:
int num = Integer.parseInt(text2);

the error generated was:

java.lang.NumberFormatException: For input string: " "

This is telling you that the String text2 contains a single space and that's it. This is not a valid integer. To handle bad input, you can use a try/catch block.
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my string has not yet been initialize till my button is pressed so how can it contain space?..
is there some other possibility for the errors?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post more of the surrounding code? How about printing out the value before converting it into an int (put |s or "s around it so you can see if it's empty or null or a space).

Um, why are you converting it before it has a value? That will always fail. Also, if it were null (uninitialized), I believe parseInt will throw a NullPointerException (read the JavaDocs to be sure).
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply.. but i manage a different way of solving it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic