0

this is my first time posting so please excuse me if i'm not being clear,

i'm tryin to buidt a program that ask for a person datas know that i already created a classe Person with it's setters and getters for all attribute, my problem is when a set a while loop in a switch case something like this

while (terminar == false) {
            System.out.println("tell me the fname of the person? " );
            datosPersona.setNombre( sc.nextLine());
          
            
            System.out.println("tell me the lname of the person? ");
            datosPersona.setApellido(sc.nextLine());
                          }

It skips through the first input and prints both quetions allowing to type only once.

output :

 tell me the fname of the person?
 tell me the lname of the person? 
(cursor here)

This is thos whole code to clarify

public class myCode {

public static void main(String[] args) throws IOException {
    int opcion = 0;
    boolean terminar = false;
    Scanner sc = new Scanner (System.in);
    Persona datosPersona = new Persona();
    List <Persona> listadePersonas = new ArrayList();
    FileWriter escritura = new FileWriter("persona.txt",true);
    
    System.out.println("make a choise");
    System.out.println("press 1 to enter  data");
    System.out.println("press 2 to save it to a file");
    System.out.println("press 0 to exist program");
    
    opcion = sc.nextInt();
    switch (opcion) {
    case 1 :
        
        while (terminar == false) {
            System.out.println("Dime el nombre de la persona " );
            datosPersona.setNombre( sc.nextLine());
          
            
            System.out.println("Dime el apellido de la persona ");
            datosPersona.setApellido(sc.nextLine());
            // añadir una persona con nombre y apellido
            listadePersonas.add(new Persona(datosPersona.nombre, datosPersona.apellido));
            //mostrar por consola
            System.out.println("Nombre: " + datosPersona.nombre);
            System.out.println("Apellido: " + datosPersona.apellido);
            
            System.out.println("If you wanna add a new person details type 'Yes', if not type 'No'");
            String opcion2 = sc.nextLine();
            if (opcion2 == "Yes" || opcion2 == "Yes" || opcion2 == "YES") {
                terminar = false;               
                
            }else {
                terminar = true;
                System.out.println("program will close");
                }
           } break;

    //case 2 :  Nothing yet
    case 0 : terminar = true;   
    System.out.println("program closed");
    System.exit(0); break;
    default  : break;
    }

}

}

  • `opcion = sc.nextInt();` is leaving a dangling new line character in the buffer, so the next call to `nextLine` consumes and returns immediately. Personally, I read the `nextLine` and then use a seperate parsing working (ie a different `Scanner` or `Integer.parseInt`) – MadProgrammer Jan 15 '22 at 23:52

0 Answers0