Error in the switch case that I do not understand

0

I have made this program that is a stack. In this case I decided to make a menu in which I called the methods but when it comes to calling them it does not work for me, it makes me wrong all the cases.

I think my mistake is when calling the methods but I do not understand what my error is. I would appreciate a help, thanks. (It is not necessary that I solve it well but I would like to understand why I do it wrong and thus not make mistakes again.)

import java.util.Scanner;
public class Pila_1 {

    class Nodo{
        int info;
        Nodo sig;
    }

    private Nodo raiz;

    public Pila_1(){
        raiz=null;
    }

    public void insertar(int x){
        Nodo nuevo;
        nuevo=new Nodo();
        nuevo.info=x;
        if(raiz!=null){
            nuevo.sig=raiz;
            raiz=nuevo;
        }
        else{
            nuevo.sig=null;
            raiz=nuevo;
        }
    }
    public int extraer(){
        if(raiz!=null){
             int informacion=raiz.info;
             raiz=raiz.sig;
             return informacion;
        }else{
             return Integer.MAX_VALUE;   
        }
    }
    public void imprimir(){
        Nodo recorrer=raiz;
        while(recorrer!=null){
            System.out.println(recorrer.info+"---");
            recorrer=recorrer.sig;
        }

    }
    public static void main(String[] args) {
        Scanner entrada=new Scanner(System.in);
        int op;
        System.out.println("Que opcion desea tomar: 1-Insertar   2-Extraer  3-Imprimir");
        op=entrada.nextInt();
        do{
            switch(op){
                case 1:
                    System.out.println("Declara una variable a introducir :");
                    int variable;
                    variable=entrada.nextInt();
                    insertar(variable);
                    break;
                case 2:
                    extraer();
                    break;
                case 3:
                    imprimir();
                    break;
            }

        }while(op==1||op==2||op==3);
    }
}
    
asked by winnie 30.05.2018 в 11:50
source

2 answers

1

The problem is that you are trying to access some methods of your class not declared as static from a method that is. The compiler is telling you that you can not do that with the error you mentioned. To solve it, you have two options:

  • Declare those methods as static : in my opinion, it is a mistake to do that since you would be making a serious design error of your objects. The static has a "viral" effect and if you choose this solution you end up converting to static all the methods you declare. Also, when you start extending the use of static, it's easy to have problems if you create multiple instances of your class. In short, you should answer "Does it make sense for someone to use this method if an instance of the class that defines it has not been created before?" If the answer is affirmative, you can declare it as static; If it's negative, you should not do it.
  • Instance your class in the main method: instead of declaring the methods as static, you leave them as you initially defined them and create an instance of Pila_1, using it to manage the execution of your program.
  • The code would look like this:

    public static void main(String[] args) {
        Pila_1 pila = new Pila_1();
        Scanner entrada=new Scanner(System.in);
        int op;
        System.out.println("Que opcion desea tomar: 1-Insertar   2-Extraer  3-Imprimir");
        op=entrada.nextInt();
        do{
            switch(op){
                case 1:
                    System.out.println("Declara una variable a introducir :");
                    int variable;
                    variable=entrada.nextInt();
                    pila.insertar(variable);
                    break;
                case 2:
                    pila.extraer();
                    break;
                case 3:
                    pila.imprimir();
                    break;
            }
    
        } while(op==1||op==2||op==3);
    }
    
        
    answered by 30.05.2018 / 12:30
    source
    -2

    Simply mark the methods to be used as static

    import java.util.Scanner;
    public class Pila_1 {
    
        class Nodo{
            int info;
            Nodo sig;
        }
    
        private Nodo raiz;
    
        public Pila_1(){
            raiz=null;
        }
    
        public static void insertar(int x){
            Nodo nuevo;
            nuevo=new Nodo();
            nuevo.info=x;
            if(raiz!=null){
                nuevo.sig=raiz;
                raiz=nuevo;
            }
            else{
                nuevo.sig=null;
                raiz=nuevo;
            }
        }
        public static int extraer(){
            if(raiz!=null){
                 int informacion=raiz.info;
                 raiz=raiz.sig;
                 return informacion;
            }else{
                 return Integer.MAX_VALUE;   
            }
        }
        public static void imprimir(){
            Nodo recorrer=raiz;
            while(recorrer!=null){
                System.out.println(recorrer.info+"---");
                recorrer=recorrer.sig;
            }
    
        }
        public static void main(String[] args) {
            Scanner entrada=new Scanner(System.in);
            int op;
            System.out.println("Que opcion desea tomar: 1-Insertar   2-Extraer  3-Imprimir");
            op=entrada.nextInt();
            do{
                switch(op){
                    case 1:
                        System.out.println("Declara una variable a introducir :");
                        int variable;
                        variable=entrada.nextInt();
                        insertar(variable);
                        break;
                    case 2:
                        extraer();
                        break;
                    case 3:
                        imprimir();
                        break;
                }
    
            }while(op==1||op==2||op==3);
        }
    }
    
        
    answered by 30.05.2018 в 11:59