java - Run-time error on usage of Integer.parseInt() -
i trying solve problem flip 2 input integers , add resultant. obvious way taking modulo , getting each digit , reconstructing back, wanted convert integers string, rendering easy flipped, , reconverting int, adding , returning answer.
code:
import java.util.*; class testclass { public static void main(string args[] ) throws exception { /* * read input stdin , provide input before running * use either of these methods input //bufferedreader bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string line = br.readline(); int n = integer.parseint(line); */ //scanner scanner s = new scanner(system.in); int n = s.nextint(); string num1= " "; string modnum1= " "; string num2= " "; string modnum2= " "; int s1=0; int s2=0; while (n!=0) { int x=s.nextint(); int y=s.nextint(); //system.out.println(x+" "+y); num1=integer.tostring(x); num2=integer.tostring(y); (int i=(num1.length()-1); i>=0; i--) { modnum1=modnum1 + num1.charat(i); } //system.out.println(modnum1); // (int j=(num1.length()-1); j>=0; j--) //{ // modnum2=modnum2 + num2.charat(j); //} s1=integer.parseint(modnum1); system.out.println(s1); } n--; } //system.out.println("hello world!"); }
this error getting:
exception in thread "main" java.lang.numberformatexception: input string: " 543" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:65) @ java.lang.integer.parseint(integer.java:569) @ java.lang.integer.parseint(integer.java:615) @ chiller.main(chiller.java:40)
line 40 line i'm using parseint
. used .trim()
hack too, still doesn't work.
the error cause because java couldn't identify numeric character among character passed integer.parseint() in case passing string start ""+" "+.... there no numeric character java knows " " . using modnum1.trim()
may remove unnecessary " " string can full , might work.
Comments
Post a Comment