Converting input String to int array with Variable length in java -
here snippet of code. want of user number variable length , convert int array. example: input: 352040 , output:{3,5,2,0,4,0}
scanner in = new scanner(system.in); system.out.println(" enter size number: "); int length = in.nextint(); system.out.println(" enter number: "); int number = in.nextint(); int[] intarray = null; string str = integer.tostring(number); ( int =0; < length ;i++) intarray[i] = integer.parseint(string.valueof(str.charat(i))); system.out.println(arrays.tostring(intarray));
this comes error(exception in thread "main" java.lang.nullpointerexception) , cant understand why.
thanks help.
you need create array first.
int[] intarray = new int[length]; // create array string str = integer.tostring(number); ( int =0; < length -1;i++) intarray[i] = integer.parseint(string.valueof(str.charat(i)));
Comments
Post a Comment