Java: Detecting if a variable is a String or an Integer -



Java: Detecting if a variable is a String or an Integer -

i'm help on bit of homework have. want user come in numerical string, convert integer. want create loop observe if user entered in wrong value such "one hundred" apposed "100".

what thinking this:

do{ numstr = joptionpane.showinputdialog("please come in year in numarical form:" + "\n(ex. 1995):"); num = integer.parseint(numstr); if(num!=integer){ tryagainstr=joptionpane.showinputdialog("entered value not acceptable." + "\npress 1 seek 1 time again or press 2 exit."); tryagain=integer.parseint(tryagainstr); } else{ *rest of code...* } }while (tryagain==1);

but don't know how define "integer" value. want see if number or not prevent crashing if user enters wrong thing.

try

int num; string s = joptionpane.showinputdialog("enter number please"); while(true) { if(s==null) break; // if press cancel exit seek { num=integer.parseint(s); break; } catch(numberformatexception ex) { s = joptionpane.showinputdialog("not number , seek again"); } }

java string

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -