Count Numbers entered in a Scanner

2

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
}
    
asked by Jaime Saiag 12.09.2016 в 19:45
source

3 answers

3

I found the solution! and by myself!

Scanner scanner = new Scanner(System.in);
int input=1;
String inputtexto= "0";
int cont=0;

inputtexto = scanner.nextLine(); 
String[] numberstexto = inputtexto.split(" ");
cont=numberstexto.length;
int numbers [] = new int [cont];

for(int o=0;o<cont;o++){
    numbers [o] = Integer.parseInt(numberstexto[o]);
    System.out.println(numbers [o]);
}

for(int i=0;i<cont;i++){
    if((numbers[i])%2 ==0)System.out.println(numbers[i]+" es PAR" );
    else System.out.println(numbers[i]+" es IMPAR");
}

System.out.println("SE VERIFICO  : " +cont +"  NÚMEROS");
    
answered by 13.09.2016 / 20:45
source
1
        int cont=0; //si desea contar cuantos numeros ingreso 
       Scanner scanner = new Scanner(System.in);

       while(scanner.hasNextInt()) {//solicita una nueva entrada
          int val  =scanner.nextInt(); 
          if(val%2 ==0)System.out.println(val+" PAR" );
          else System.out.println(val+ " IMPAR");
          cont++;
       }
    System.out.println("SE VERIFICO  : " +cont +"  NÚMEROS");
    
answered by 13.09.2016 в 18:25
-1

You do not have to do anything in your Java code for Scanner.hasNext() to have the value false .

You just have to write an EOF (End Of File) and Scanner.hasNext() will return false .

In Linux, if you execute from a console the way to write an EOF is by pressing CTRL + D

Try this program:

import java.util.Scanner;
public class ScannerFin {
  public static void main(String[] args) {
    Scanner scanner = new Scanner( System.in );
    while ( scanner.hasNext() ) {
      String cadena = scanner.nextLine();
      System.out.println("Cadena leida = " + cadena);
    }
    System.out.println("Fin del programa");
  }
}

Compile it, run it, write some lines and press CTRL + D:

  

jose2 @ HP-Black: ~ $ javac ScannerFin.java
  jose2 @ HP-Black: ~ $ java ScannerFin
  hello
  Chain read = hello
  twenty seven
  Chain read = twenty-seven

Here I press CTRL + D

  

End of the program
  jose2 @ HP-Black: ~ $

    
answered by 12.09.2016 в 22:18