error when entering data by console using the Scanner class [duplicated]

0

I'm new in java I was practicing conditional and I wanted to do it more interactive but I have a problem and I think it has to do with the Scanner class it happens that the second time that he asks to enter the name and the score shows it in a single line and if I put a name and then a number that error comes out here I leave the code

package Capitulo_4;
//importacion
import java.util.Locale;
import  java.util.Scanner;

public class PruebaEstudiante {
    public static void main(String[] args) {
        //VARIABLES LOCALES
        String name;
        double puntos;

        //CREACION E INSTANCIAMIENTO DE OBJETOS DE LA CLASE ESTUDIANTE
        Estudiante cuenta1 = new Estudiante("joel", 16.25);
        Estudiante cuenta2 = new Estudiante("lesley", 100.00);
        Scanner entrada = new Scanner(System.in).useLocale(Locale.US);

        //IMPRIMIR
        System.out.print("ingrese su nombre: ");
         name = entrada.nextLine();
        System.out.print("ingrese el puntaje obtenido: ");
         puntos = entrada.nextDouble();

         cuenta1.establecerNombre(name);
         cuenta1.establecerPromedio(puntos);

        System.out.printf("Registrando datos de la cuenta1 %n%s%s%n%s%.2f%n%n",
                "   >ingresando nombre: ",cuenta1.obtenerNombre(),
                "   >ingresando puntaje : ",cuenta1.obtnerPromedio());

        System.out.print("ingrese su nombre: ");
        name = entrada.nextLine();

        System.out.print("ingrese el puntaje obtenido: ");
        puntos = entrada.nextDouble();

        cuenta2.establecerNombre(name);
        cuenta2.establecerPromedio(puntos);

        System.out.printf("Registrando datos de la cuenta2 %n%s%s%n%s%.2f%n%n",
                "   >ingresando nombre: ",cuenta2.obtenerNombre(),
                "   >ingresando puntaje : ",cuenta2.obtnerPromedio());

        System.out.printf("La calificacion en  letra de %s es : %s%n",cuenta1.obtenerNombre(),cuenta1.obtenerCalificación());
        System.out.printf("La calificacion en  letra de %s es : %s%n",cuenta2.obtenerNombre(),cuenta2.obtenerCalificación());
    }
}

I can leave a picture so you can see the error that comes out

    
asked by Vladimir Joel 01.11.2017 в 07:27
source

1 answer

1

This is because when numeric values (of any kind) have been entered or read with the methods nextInt() , nextDouble() ... leave the character \ n or enter in the buffer.

When you want to read a string with the 'nextLine ()' method, this function starts reading the buffer and the first thing you find is just the string end character \ n , it is there when the flow jumps to the next line, and although we think that nothing was read, if that character was read. In your case, the problem is extended and, as it continues, it generates unexpected behavior.

To fix it, after calling the method of the class Scanner nextInt() or nextDouble() you must call the method nextLine() . For example just before the second data entry:

   //CREACION E INSTANCIAMIENTO DE OBJETOS DE LA CLASE ESTUDIANTE
    Estudiante cuenta1 = new Estudiante("joel", 16.25);
    Estudiante cuenta2 = new Estudiante("lesley", 100.00);
    Scanner entrada = new Scanner(System.in).useLocale(Locale.US);

    //IMPRIMIR
    System.out.print("ingrese su nombre: ");
     name = entrada.nextLine();
    System.out.print("ingrese el puntaje obtenido: ");
     puntos = entrada.nextDouble();

     cuenta1.establecerNombre(name);
     cuenta1.establecerPromedio(puntos);

    System.out.printf("Registrando datos de la cuenta1 %n%s%s%n%s%.2f%n%n",
            "   >ingresando nombre: ",cuenta1.obtenerNombre(),
            "   >ingresando puntaje : ",cuenta1.obtnerPromedio());

    entrada.nextLine(); // Limpiando el buffer 
    System.out.print("ingrese su nombre: ");
    name = entrada.nextLine();

    System.out.print("ingrese el puntaje obtenido: ");
    puntos = entrada.nextDouble(); // Si después de esto volveremos a tener residuo en el buffer 

    cuenta2.establecerNombre(name);
    cuenta2.establecerPromedio(puntos);

    System.out.printf("Registrando datos de la cuenta2 %n%s%s%n%s%.2f%n%n",
            "   >ingresando nombre: ",cuenta2.obtenerNombre(),
            "   >ingresando puntaje : ",cuenta2.obtnerPromedio());

    System.out.printf("La calificacion en  letra de %s es : %s%n",cuenta1.obtenerNombre(),cuenta1.obtenerCalificación());
    System.out.printf("La calificacion en  letra de %s es : %s%n",cuenta2.obtenerNombre(),cuenta2.obtenerCalificación());
    
answered by 01.11.2017 в 08:29