java - Number Format Exception Error logic help don't see the issue -
getting number format exception error when trying design , exit command in expression string. advice tremendously appreciated :).
string expression = " "; //all other varaible in loop declared , intialized //removed cluttering purposes while(!expression.equals("quit")) { expression = kbd.nextline(); variable = expression.charat(0); startindex = expression.lastindexof(" ")+1;//index of future integer value stringlength = expression.length(); strvalue = expression.substring(startindex,stringlength); intvalue = integer.parseint(strvalue);//error here numberformatexception } system.out.println(" "+strvalue);//string value test print tempindex.put(variable,intvalue);//assigns int value variable int getvarvalue = tempindex.get(variable);//gets value stored in variable system.out.println(variable+" = "+getvarvalue);
java complains not giving integer here:
intvalue = integer.parseint(strvalue);
for instance if run
integer.parseint("1a");
i error parseint
not make 1
you, throw exception saying not know in case,
you can try verifying string first before trying parse it. regular expressions come handy here. if not know regular expressions check chars in string either '0', '1', '2', ... '9'. if else encounter not call parseint
method
Comments
Post a Comment