java - How to put two numbers separated by a space into two different arrays -
i have 2 number inputs
1010 212
i want put these 2 different arrays ([1 0 1 0] , [2 1 2])
this code
import java.io.*; import java.util.*; public class solution { public static void main(string[] args) { int array[] = new int[8]; int array2[] = new int[8]; int = 0; scanner getnumber = new scanner(system.in); { array[i] = getnumber.nextint(); i++; } while(getnumber.hasnextint()); system.out.println(arrays.tostring(array)); } }
but output
[1010, 212, 0, 0, 0, 0, 0, 0]
(i put 8 generic array length because java compiler forced initialize each array)
you need store numbers first initialize arrays based on length of number. can length turning string. once left number , empty array, can split integer array of digits number of ways.
int[] array, array2; scanner in = new scanner(system.in); int a, b; = in.nextint(); b = in.nextint(); array = new int[integer.tostring(a).length()]; array2 = new int[integer.tostring(b).length()]; int = 0; while(a > 0) { array[i] = % 10; /= 10; i++; } i=0; while(b > 0) { array2[i] = b % 10; b /= 10; i++; } system.out.println(arrays.tostring(array)); system.out.println(arrays.tostring(array2));
Comments
Post a Comment