I want to make a program in Java that reads a line from the standard input using the class Scanner
.
The user will write a line with numbers separated by spaces; for example:
3 10 0 13 4
The program must count how many numbers have been entered into the line. And process those numbers in a loop to print which are odd and even.
The output for the previous example would be:
3 es IMPAR
10 es PAR
0 es PAR
13 es IMPAR
4 es PAR
SE VERIFICO : 5 NÚMEROS
I have tried to read the numbers with the following program but the problem is that it does not leave the while loop when the user presses ENTER to indicate end of line. This is the program that does not work:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numerosLeidos = 0;
while (scanner.hasNext()) {
int numero = scanner.nextInt();
System.out.println("Se ha introducido el numero : " + numero);
++numero;
}
System.out.println("Se han leido : " + numerosLeidos ); // Nunca se llega a esta linea
}