input - This java program produce unwanted results -
i wrote program read input , print out.
public class inverse { public static void main (string arg[]) throws ioexception { int input1 = system.in.read(); system.out.println(input1); string temp= integer.tostring(input1); system.out.println(temp); int[] numtoarray =new int[temp.length()]; system.out.println(temp.length()); (int i=0 ;i<temp.length(); i++) {numtoarray[i]= temp.charat(i); system.out.println(numtoarray[i]+"*"); } }}
but here when write 123456 print 49. should print 123456. cause problem?
123456
integer, system.in.read()
reads next byte input not read integer expected. use scanner#nextint()
method read integer:
scanner input = new scanner(system.in); int input1 = input.nextint();
your numtoarray
array print bytes, not individual characters of integer parsed string. print characters, change type char[]
:
char[] numtoarray = new char[temp.length()]; system.out.println(temp.length()); (int = 0; < temp.length(); i++) { numtoarray[i] = temp.charat(i); system.out.println(numtoarray[i] + "*"); }
Comments
Post a Comment