Problem with Java scanner

1

Good morning, I'm doing a small project to work with hospital patients. My problem arises when I want to receive data by keyboard. What code do I have?

_       //Get nombre
        System.out.println("Introduzca nombre del paciente");
        Scanner name_scanner = new Scanner (System.in);
        nombre=name_scanner.nextLine();
        name_scanner.close();

        //Get apellido 1
        System.out.println("Introduzca primer apellido del paciente");
        Scanner apell1_scanner = new Scanner (System.in);
        apell1=apell1_scanner.nextLine();
        apell1_scanner.close();

        //Get apellido 2
        System.out.println("Introduzca segundo apellido del paciente");
        Scanner apell2_scanner = new Scanner (System.in);
        apell2=apell2_scanner.nextLine();
        apell2_scanner.close();

        //Get edad
        System.out.println("Introduzca la edad del paciente");
        Scanner edad_scanner = new Scanner(System.in);
        edad=edad_scanner.nextInt();
        edad_scanner.close();

        //Get alcohol
        System.out.println("¿El paciente toma alcohol? Inserte Si - No");
        Scanner alcohol_scanner = new Scanner(System.in);
        alcohol= alcohol_scanner.nextLine();
        alcohol_scanner.close();

        //Get fumador
        System.out.println("¿El paciente fuma? Inserte Si - No");
        Scanner fumador_scanner = new Scanner(System.in);
        fumador= fumador_scanner.nextLine();
        fumador_scanner.close();

        //Get Numero Historial Clinico
        System.out.println("Introduzca el número de historial clinico del              paciente");
        Scanner numhistorialclinico = new Scanner(System.in);
        num_historial_clinico=numhistorialclinico.nextInt();
        numhistorialclinico.close();

        //Get Diagnostico
        System.out.println("Introduzca observaciones/comentarios/diagnóstico del paciente");
        Scanner sc_diagnostico = new Scanner(System.in);
        diagnostico= sc_diagnostico.nextLine();
        sc_diagnostico.close();

        Paciente.RegisterPaciente(nombre, apell1, apell2, edad, alcohol,  fumador, num_historial_clinico, diagnostico);
    }

The problem I have is that I get the following error:

_Por favor presione el número de la acción a realizar
 1 - Ingresar datos nuevo paciente
 2 - Consultar datos paciente
 3 - Eliminar paciente
 4 - Salir del programa
 1
 Introduzca nombre del paciente
 Exception in thread "main" java.util.NoSuchElementException: No line found
 at java.util.Scanner.nextLine(Unknown Source)
 at MenuPacientes.getOption(MenuPacientes.java:46)
 at Main_Control_Pacientes.main(Main_Control_Pacientes.java:7)

Well, this is the problem, which prints the menu well, pulse 1 to register patient, print the name, but then it explodes. What am I doing wrong? Each scanner that I use I open it, I use it, and I close it. Any help pls?

    
asked by Keka Bron 31.03.2017 в 20:32
source

2 answers

5

The concrete problem is with your calls to .close() with the scanners. Every time you run the .close() to Scanner , this also executes .close() to System.in , which invalidates your other scanners that try to read the System.in .

Actually, part of the problem is that there's no reason why you create a different instance of Scanner every time you need to interact with the user. You only need an instance of Scanner that you can continue to use throughout your program (without closing it of course).

    
answered by 31.03.2017 / 20:43
source
0

For what you told sstan in your answer, it is not recommended to create crowds of Scanner . While managing your income with only Scanner keep in mind that all methods nextXXX apart from nextLine do not consume the "\s" that separates the data:

    Scanner scanner = new Scanner (System.in);
    //Get nombre
    System.out.println("Introduzca nombre del paciente");
    nombre=scanner.nextLine();

    //Get apellido 1
    System.out.println("Introduzca primer apellido del paciente");
    apell1=scanner.nextLine();

    //Get apellido 2
    System.out.println("Introduzca segundo apellido del paciente");
    apell2=scanner.nextLine();

    //Get edad
    System.out.println("Introduzca la edad del paciente");
    edad=scanner.nextInt();
    scanner.nextLine(); // consumir \s

    //Get alcohol
    System.out.println("¿El paciente toma alcohol? Inserte Si - No");
    alcohol= scanner.nextLine();

    //Get fumador
    System.out.println("¿El paciente fuma? Inserte Si - No");
    fumador= scanner.nextLine();

    //Get Numero Historial Clinico
    System.out.println("Introduzca el número de historial clinico del              paciente");
    num_historial_clinico=scanner.nextInt();
    scanner.nextLine(); // consumir \s

    //Get Diagnostico
    System.out.println("Introduzca observaciones/comentarios/diagnóstico del paciente");
    Scanner sc_diagnostico = new Scanner(System.in);
    diagnostico= scanner.nextLine();

    Paciente.RegisterPaciente(nombre, apell1, apell2, edad, alcohol,  fumador, num_historial_clinico, diagnostico);
    
answered by 31.03.2017 в 22:23