0

I am new in data structures and I have been writing a program that uses stacks and show it using a switch case. The first input works fine, it stores data in the stack perfectly, but the problem is in the second round because the code ignores my first input, suddenly it goes to the second input.

EDIT I just put a sc.nextline() at the end of the stack in agregar() and it worked.

Example: --First round-- name "Diego" last name "Samayoa" phone "123" id "654"

--Second round-- name HERE IS WHEN THE CODE IGNORES THE INPUT last name "Samayoa" phone "123" id "654"


import java.util.Scanner;
import java.util.Stack;

public class Alumno {

    Scanner sc = new Scanner(System.in);
    Stack<Alumno> pila = new Stack<>();
    private String nombre;
    private String apellido;
    private int telefono;
    private int carne;

    public Alumno() {
    }

    public Alumno(String nombre, String apellido, int telefono, int carne) {
        this.nombre = nombre;
        this.apellido = apellido;
        this.telefono = telefono;
        this.carne = carne;
    }

    public int getCarne() {
        return carne;
    }

    public void setCarne(int carne) {
        this.carne = carne;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public int getTelefono() {
        return telefono;
    }

    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }
    
    public void Agregar() {
        System.out.println("--Agregar nuevo alumno--");
                   
        System.out.println("Ingrese Nombre: ");
        nombre = sc.nextLine();
        setNombre(nombre);

        System.out.println("Ingrese apellido: ");
        apellido = sc.nextLine();
        setApellido(apellido);

        System.out.println("Ingrese telefono: ");
        telefono = sc.nextInt();
        setTelefono(telefono);

        System.out.println("Ingrese carné: ");
        carne = sc.nextInt();
        setCarne(carne);

        Alumno registro = new Alumno(getNombre(), getApellido(), getTelefono(), getCarne());
        Alumno push = pila.push(registro);
// pila.push(registro);

    }

    public void EliminarReciente() {
        System.out.println("--Eliminar dato más reciente--");
        System.out.println("Valor eliminado: " + pila.pop());
System.out.println("");
    }

    public void MostrarReciente() {
        System.out.println("--Mostrar dato más reciente--");
        System.out.println("Valor más reciente: " + pila.peek());
        System.out.println("");

        //System.out.println(pila.toString());
    }

    public void MostrarTotal() {
        System.out.println("--Mostrar pila completa--");
        System.out.println("Pila resultante: ");
        pila.forEach(k -> {
            System.out.println(k);
            System.out.println("");
        });
    }

    public void VaciarPila() {
        System.out.println("--Vaciar pila--");
        pila.clear();
        System.out.println("Pila eliminada, presione para continuar.");
        //sc.nextLine();
        System.out.println("");
    }

    @Override
    public String toString() {
        return String.format("%nNombre: %s%nApellido: %s%nTelefono: %s%nCarné: %s%n"
                + "", getNombre(), getApellido(), getTelefono(), getCarne());
    }

}


    
import java.util.Scanner;
import java.util.*;

public class Principal {

    public static void main(String[] args) {
        Alumno acciones = new Alumno();
        Scanner sc = new Scanner(System.in);
        int opc;

        do {
            System.out.println("--Menú de Inicio--");
            System.out.println("1. Agregar alumno");
            System.out.println("2. Eliminar dato más reciente");
            System.out.println("3. Mostrar dato más reciente");
            System.out.println("4. Mostrar pila completa");
            System.out.println("5. Vaciar pila");
            System.out.println("6. Salir");

            System.out.print("Ingrese opción: ");
            opc = sc.nextInt();

            switch (opc) {

                case 1: {
                    acciones.Agregar();
                    break;
                }

                case 2: {
                    acciones.EliminarReciente();
                    break;
                }

                case 3: {
                    acciones.MostrarReciente();
                    break;
                }

                case 4: {
                    acciones.MostrarTotal();
                    break;
                }

                case 5: {
                    acciones.VaciarPila();
                    break;
                }

                case 6: {
                    System.out.println("Presione una tecla para finalizar.");
                    sc.nextLine();
                    break;
                }
                
                default: {
                    System.out.println("La opción no es válida.");
                }
            }

        } while (opc != 6);
        System.exit(0);

    }

}

  • 1
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – OH GOD SPIDERS Feb 16 '21 at 17:44
  • You need to move `sc` and `pila` out of the `Alumno` class. Or make them `static`. Having an instance of `Alumno` being the owner of the stack, as well as representing a person, it confusing and just plain wrong. – Andreas Feb 16 '21 at 17:45

0 Answers0