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?