java - Conditions regarding parsed integer from JTextField -


in program value of jtextfield , parse integer:

string string = jtext.gettext(); int stringval = integer.parseint(str); 

my question how i'm able check if value parsed integer? tried this, didn't hold results wished accomplished , received errors.

if(str == null)   {       joptionpane.showmessagedialog(null, "integer not parsed", "message",       joptionpane.information_message);   } 

if wasn't able parsed int throw exception.

try using try catch statement

boolean tryparseint(string value)   {        try        {            integer.parseint(value);            return true;         } catch(numberformatexception nfe)         {             return false;         }   } 

you use this

if(tryparseint(myinput)) {      integer.parse(myinput);  } 

or if want without method

int stringval; bool success = true;  try {   stringval = integer.parseint(str); } catch (numberformatexception nfe)   {   success = false;   joptionpane.showmessagedialog(null, "integer not parsed", "message",   joptionpane.information_message); }  if (success) {   //do whatever } 

hope helps


Popular posts from this blog