Why don't I have to use nextLine when entering numbers into console on separate lines? I expected console to interpret end of each line having \n but program works the same whether I use nextLine() after each nextInt(). Using Eclipse.
// Example used
10 5 // # lines to read , divisor
22 // if # divisible by divisor, count++
15
10
25
17
13
15
10
7
9
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n = getNumber(keyboard);
int k = getNumber(keyboard);
int numLinesPassed = getNumberPassCriteria(keyboard, n, k);
System.out.println("# Passed: " + numLinesPassed);
}
public static int getNumber(Scanner keyboard) {
return keyboard.nextInt();
}
public static int getNumberPassCriteria(Scanner keyboard, int n, int k) {
int counter = 0;
for(int i = 0; i < n; i++) {
int value = keyboard.nextInt();
if (value % k == 0) {
counter++;
}
//keyboard.nextLine(); not understand why I don't need this
}
return counter;
}