java - Exception when converting string into integer -
i'm starter in java. use netbeans. following snippet code make simple calculator add 2 numbers. not including self generated code buttons.
problem
when try convert string str
integer num1
inside function plus_buttonactionperformed
, equal_buttonactionperformed
gives exception stating:
exception in thread awt-eventqueue-0 java.lang.numberformatexception input string
i made sure string not empty printing above conversion statement. code bit long. pardon. doing wrong here.
public class calc extends javax.swing.jframe { /** * creates new form calc */ public string str = " "; public string action = " "; public int num1; public int num2; public int res; public calc() { initcomponents(); } private void button3actionperformed(java.awt.event.actionevent evt) { str=str.concat("3"); result.settext(str); } private void button6actionperformed(java.awt.event.actionevent evt) { str=str.concat("6"); result.settext(str); } private void button9actionperformed(java.awt.event.actionevent evt) { str=str.concat("9"); result.settext(str); } private void resultactionperformed(java.awt.event.actionevent evt) { result.settext(str);// } private void button1actionperformed(java.awt.event.actionevent evt) { str=str.concat("1"); result.settext(str);// } private void button2actionperformed(java.awt.event.actionevent evt) { str=str.concat("2"); result.settext(str); } private void button4actionperformed(java.awt.event.actionevent evt) { str=str.concat("4"); // result.settext(str); } private void button5actionperformed(java.awt.event.actionevent evt) { str=str.concat("5"); // result.settext(str); } private void button7actionperformed(java.awt.event.actionevent evt) { str=str.concat("7"); result.settext(str);// } private void button8actionperformed(java.awt.event.actionevent evt) { str=str.concat("8"); result.settext(str);// } private void plus_buttonactionperformed(java.awt.event.actionevent evt) { system.out.println(str); num1=integer.parseint(str); system.out.println(num1); str=" "; } private void equal_buttonactionperformed(java.awt.event.actionevent evt) { num2=integer.parseint(str); res=num1+num2; str=""+res; result.settext(str); } public static void main(string args[]) { try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(calc.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(calc.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(calc.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(calc.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new calc().setvisible(true); } }); }
your string str = " ";
has space @ start , concating in it, use trim()
or set
string str="";
(suggested rudi
, me too)
string str=" "; ^
to survive kind of things better use trim()
remove leading , trailing sapces string
.
morover suggest check string
before parsing null
check , empty
check
if(str ==null || str.trim().equals("")){ //than don't parse }
problem : can not use concat
on null
string
should empty(atleast) or set "0"
or "1"
or integer parseable.
Comments
Post a Comment