Is there an equivalent to cin.clear () and cin.ignore () in Java?

3

I have a method that helps me to validate variables int , but when I pass them from C ++ to Java the ignore and clear methods do not work for me.

Is there any equivalent to those functions in Java ?

int ValidaInt(){

    int numero;

    if (cin>>numero)
    {
        return numero;
    }
    else
    {
        cout<<"El caracter digitado no corresponde a un numero\n";
        cout<<"Por favor digite un numero:\n";
        cin.clear(256,"\n");
        cin.ignore();
        validaInt();
    }
}
    
asked by Kevin Mendez 14.05.2017 в 02:23
source

1 answer

1

to validate the entry of a number in java would be something like this:

 Scanner sc = new Scanner(System.in);
    int numero;
    do {
        System.out.println("Dame un numero");
        while (!sc.hasNextInt()) {
            System.out.println("Te dije un numero");
            sc.next(); //esperar el siguiente input
        }
            numero= sc.nextInt();
    } while (numero<= 0);
  System.out.println(numero);
    
answered by 14.05.2017 в 03:07