Hi I am new to Java and I was experimenting with the Scanner class. I am trying to figure out a small problem in which I want to enter two inputs such as: 4 5 6 and 8 9 0. I want to store 4,5,6 in one array and 8,9,0 in another array and then print these arrays. But I am unable to do so. I wrote the following code :
public class scanner {
public static void main(String[] args) {
int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);
int i = 0;
while(scan.hasNextInt()){
array[i] = scan.nextInt();
i++;
if(i == 3){
break;
}
}
i = 0;
while(scan.hasNextInt()){
array2[i] = scan.nextInt();
i++;
if(i == 3){
break;
}
}
for(int j = 0; j < array.length; j++){
System.out.println(array[j]);
}
for(int j = 0; j < array2.length; j++){
System.out.println(array2[j]);
}
}
}
But this doesn't takes the input 4 5 6 in one single line. I want to enter 4 5 6 in one line so that all the three digits are stored in the array. Can someone please help me. I assume I should use delimiter to remove the white space but I am not sure how to go about it.