Issue
I want to read multiple integers from 1 line of input, i know there are more questions on this topic but none are applicable in my situation, so please don't delete this question. i want a code that allows multiple integers to be input on a single line, and be stored separately in an int array. The input can be 1-13 integers. so it could be 2 3 4, or it could be 7 or it could be 2 2 5 8. I tried this:
while(scanner.hasNextInt()){
ida[k] = scanner.nextInt();
k++;
}
but it never stops asking for integers, my code just stops here and you have to keep putting more integers in.
Solution
A small example which splits after a blank, so a example input could be:
---> 3 4 9 10
String input = scanner.nextLine();
String integers[] = input.split(" ");
if(integers.length > 13 || integers.length < 1){
//ErrorHandling
}
for(String number : integers){
try {
int num = Integer.parseInt(number);
//Add to array
} catch(NumberFormatException e){
//number String input was not a number
}
}
Answered By - DZDomi