Error fixing objects in java

1

I have a question about this error (I'm starting with java):

  

Exception in thread "main" java.lang.RuntimeException: Uncompilable   source code - Erroneous tree type: principal.Patent at   principal.Principal.main (Principal.java.20)   C: \ Users \ Pedro \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53:   Java returned: 1 BUILD FAILED (total time: 0 seconds)

Here my code: Principal.java :

package principal;

import java.util.Scanner;

public class Principal {

    public static void main(String[] args) {
        Scanner leer = new Scanner(System.in);
         /*
         a[0].SetMatricula(12);
         a[0].SetNombre("Juan");
         a[0].getMatricula();
         */

        Paciente a[] = new Paciente[5];
        String nombre = "";
        int matricula = 0;
        char grupo = ' ';
        int grado = 0;

        for (int i = 0; i < a.length; i++) {
            System.out.println("Ingresa tu nombre");
            nombre = leer.nextLine();
            System.out.println("Ingresa tu matricula");
            matricula = leer.nextInt();
            System.out.println("Ingresa tu grupo");
            grupo = leer.next().charAt(0);
            System.out.println("Ingresa tu grado");
            grado = leer.nextInt();
            leer.nextLine();
            a[i] = new Paciente(nombre, matricula, grupo, grado);
        }
    }
}

Class Paciente :

public class Paciente {
    private String nombre;
    private int matricula;
    private char grupo;
    private int grado;


    public Paciente() {
        nombre = "";
        matricula = 0;
        grupo = ' ';
        grado = 0;
    }

    public Paciente(String n, int m, char g, int gg) {
        nombre = "";
        matricula = 0;
        grupo = ' ';
        grado = 0;
    }

    public String getNombre() {
        return nombre;
    }

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

    public int getMatricula() {
        return matricula;
    }

    public void setMatricula(int matricula) {
        this.matricula = matricula;
    }

    public char getGrupo() {
        return grupo;
    }

    public void setGrupo(char grupo) {
        this.grupo = grupo;
    }

    public int getGrado() {
        return grado;
    }

    public void setGrado(int grado) {
        this.grado = grado;
    }
}
    
asked by Pedro Robles 28.09.2017 в 01:44
source

2 answers

1

If really you need two contributors in your class Paciente they should be like this:

   public Paciente()
   {
        this.nombre="";
        this.matricula=0;
        this.grupo=' ';
        this.grado=0;
    }

   public Paciente(String nombre,int matricula,char grupo,int grado)
   {
        this.nombre=nombre;
        this.matricula=matricula;
        this.grupo=grupo;
        this.grado=grado;
   }

If you do not use this the members do not acquire the values that you pass to them when doing:

new Paciente(nombre,matricula,grupo,grado);

Also, to show the data of your class Paciente it would be good if the skills of a toString method:

For example:

@Override
public String toString(){
    return 
            "Nombre: "+this.nombre
            +" Matricula:"+this.matricula
            +" Grupo: "+this.grupo
            +" Grado: "+this.grado;
}

Class% complete_co%:

public class Paciente 
{
    private String nombre;
    private int matricula;
    private char grupo;
    private int grado;


    public Paciente()
   {
        this.nombre="";
        this.matricula=0;
        this.grupo=' ';
        this.grado=0;
    }

   public Paciente(String nombre,int matricula,char grupo,int grado)
   {
        this.nombre=nombre;
        this.matricula=matricula;
        this.grupo=grupo;
        this.grado=grado;
   }


    public String getNombre() {
        return nombre;
    }

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

    public int getMatricula() {
        return matricula;
    }

    public void setMatricula(int matricula) {
        this.matricula = matricula;
    }

    public char getGrupo() {
        return grupo;
    }

    public void setGrupo(char grupo) {
        this.grupo = grupo;
    }

    public int getGrado() {
        return grado;
    }

    public void setGrado(int grado) {
        this.grado = grado;
    }

    @Override
    public String toString(){
        return 
                "Nombre: "+this.nombre
                +" Matricula:"+this.matricula
                +" Grupo: "+this.grupo
                +" Grado: "+this.grado;
}

}

If you want to see the data in the class, put this out of the% Paciente c of the Scanner:

 System.out.println(Arrays.toString(a));
    
answered by 28.09.2017 / 02:11
source
2

The error can be due to data types. It is correct what you mention A. Cedano in the declaration of the builders, however I would declare only one for the moment, the following

 public Paciente()
 {
    this.nombre="";
    this.matricula=0;
    this.grupo=' ';
    this.grado=0;
 }

As for the implementation, apparently you are declaring the arrangement wrong, it should be:

  Paciente[] a = new Paciente[5];

Try this example. It is no longer necessary to declare the variables in the Main again, make an instance of the Patient class, fill in the fields directly when the user enters them and assign them to your Patient arrangement.

    public class Principal
    {
        public static void main(String[] args) 

        {
            Scanner leer = new Scanner(System.in);
            Paciente[] a = new Paciente[5];


            for(int i=0;i<a.length;i++)
            {
                Paciente p = new Paciente();
                System.out.println("Ingresa tu nombre");
                p.nombre=leer.nextLine();
                System.out.println("Ingresa tu matricula");
                p.matricula=leer.nextInt();
                System.out.println("Ingresa tu grupo");
                p.grupo=leer.next().charAt(0);
                System.out.println("Ingresa tu grado");
                p.grado=leer.nextInt();
                leer.nextLine();
                a[i]= p;
            }

         //Si quieres mostrar los elementos que tiene tu arreglo
         Console.WriteLine(a[0].nombre + " " + a[0].matricula + " " + a[0].grupo + " " + a[0].grado);
        }
    }

It can be done in a cleaner way using lists instead of fixes. I hope it is useful for you. Greetings.

    
answered by 28.09.2017 в 04:28