-2

The last line of the code below gives the following error: " expected

illegal start of type

package arrEmpleados does not exist"

package javaPrueba2;

import java.util.ArrayList;

class Principal {
    ArrayList<Empleado> arrEmpleados = new ArrayList<Empleado>(); 

arrEmpleados.add(new Empleado(541000, 2400.0, 40.0, 16, 20.0, 2));

Empleado is a class in another file. That one, this one and the main are all in the package javaPrueba2 displayed in the 1st line...

The same error is returned if I just create a simple string ArrayList

ArrayList<String> arrEmpleados = new ArrayList(); 
arrEmpleados.add("ana"));

What's going on? I almost copied the way to do the ArrayList from here: StackOverflow: Creating an Arraylist of Objects

So if this is wrong, how should I do it?

EDIT: Per request, this is the Empleado Class:

package javaPrueba2;


public class Empleado{
    int nif, horasExtra, numHijos; // horasExtra son mensuales
    double sueldoBase, pagoHora, porcIRPF;

    Empleado(int nif, double sueldoBase, double pagoHora, int horasExtra, double porcIRPF, int numHijos){
        this.nif = nif;
        this.sueldoBase = sueldoBase;
        this.pagoHora = pagoHora;
        this.horasExtra = horasExtra;
        this.porcIRPF = porcIRPF;
        this.numHijos = numHijos;
        }


    //Calcula el complemento por horas extras realizadas
    double complemento(){
        return pagoHora * horasExtra;
    }

    //Calcula el sueldo bruto 
    double bruto(){
        return sueldoBase + this.complemento();
    }

    //Calcula las retenciones

    double retenciones(){
        int puntosDescuento;
        double porcentajeFinal;
        if (numHijos > 2){
            puntosDescuento = 2;
        } else puntosDescuento = 1;

        porcentajeFinal = porcIRPF - puntosDescuento;

        return porcentajeFinal * this.bruto() / 100;
    }

}
Martin
  • 414
  • 7
  • 21
  • 2
    You should add commands to methods and not the class declaration. – CannedMoose Jun 12 '18 at 10:31
  • Where is your Empleado class? Post that as well. – Adya Jun 12 '18 at 10:33
  • By the way. Is this not the type of question for Stack Exchange? Are the question expected to be for programmers experienced in the language of the question? Shoulf I have stated it differently? – Martin Jun 12 '18 at 14:09
  • Oh, now reading this having slept almost normally, I see I was totally blind. Shall I just delete this question? I don't think it will help anyone... – Martin Jun 20 '18 at 18:24

1 Answers1

3

You should start to learn Basics from Java, instructions can only be in methods (or static block)

class Principal {
    ArrayList<Empleado> arrEmpleados = new ArrayList<Empleado>(); 

    public void addAEmpleado(){
        arrEmpleados.add(new Empleado(541000, 2400.0, 40.0, 16, 20.0, 2));
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thanks, that's what I'm learning, the basics... But your "hint" does indeed help me (and hopefully other beginners too...)... :) – Martin Jun 12 '18 at 10:41