0

when I create this hashMap in main an fill it in, everything works perfect

    HashMap<String, Furgoneta> mapper = new HashMap<>();
    LocalDate f1 = LocalDate.of(2013, Month.JANUARY, 1);
    LocalDate f2 = LocalDate.of(2016, Month.FEBRUARY, 10);
    Furgoneta furgo1 = new Furgoneta("0001AAA", "Seat", "Ibiza", false, f1, f2, 3100);


    //FILLING HASHMAP
    mapper.put(furgo1.getMatricula(), furgo1);
    System.out.println(mapper.toString());

when I try to write this same code inside a class instead of main the put won't work (unable to fill hashmap)

    //FILLING HASHMAP
    mapper.put(furgo1.getMatricula(), furgo1);
    System.out.println(mapper.toString());

object furgoneta (means van)

public class Furgoneta extends VehiculoAlquiler implements Serializable    
{
    private int mma;


    //constructor
    public Furgoneta(String matricula, String marca, String modelo, boolean alquilado, 
            LocalDate fechaI, LocalDate fechaF,int mma ) {
        super(matricula, marca, modelo, alquilado, fechaI, fechaF);
        this.mma = mma;
    }

    public int getMma() {
        return mma;
    }

if I write this main it works

    HashMap<String, Furgoneta> furgonetas = new HashMap<>();
    LocalDate f1 = LocalDate.of(2013, Month.JANUARY, 1);
    LocalDate f2 = LocalDate.of(2016, Month.FEBRUARY, 10);
    Furgoneta furgo1 = new Furgoneta("0001AAA", "Seat", "Ibiza", false, f1, f2, 3100);


    //Las introducimos en la HashMap
    furgonetas.put(furgo1.getMatricula(), furgo1);
    System.out.println(furgonetas.toString());


    furgo1.importeAlquiler(f1, f2);

but I want to do the exact same but with the hashMap inside a class not in main

I cannot fill in the hashmap I created. If the hashmap is inside main I can fill it in but if I try to write the hashmap inside a class instead of main and fill it, when i'm trying to fill it in it says "package mapper does not exist" and "package furgo1 does not exit" when mapper is meant to be the hashmap name and furgo1 an object

i cannot see my mistake, i would really appreciate some help, i been stuck here for hours. Sorry if is confusing is the first time i ask

Bea
  • 15
  • 4
  • Can you elaborate, what do you mean by "cannot fill", also could you post a [mcve](http://stackoverflow.com/help/mcve)? –  May 02 '16 at 14:36
  • Post your class code – Nadir May 02 '16 at 14:36
  • I would guess you are not constructing or setting up your class properly @Bea. But you really need to explain more of what is going wrong. The error message / control flow you are going through. Nothing in Java [lives outside of classes](http://stackoverflow.com/a/2155407/703644) so saying "this doesn't work in a class" is also sort of a misnomer FYI – TheNorthWes May 02 '16 at 14:39
  • you need clear your java basics..if you wish to do that then it could be done inside a static block inside class – Mrunal Gosar May 02 '16 at 14:41
  • public class Furgoneta extends VehiculoAlquiler implements Serializable { private int mma; //constructor public Furgoneta(String matricula, String marca, String modelo, boolean alquilado, LocalDate fechaI, LocalDate fechaF,int mma ) { super(matricula, marca, modelo, alquilado, fechaI, fechaF); this.mma = mma; } public int getMma() { return mma; } – Bea May 02 '16 at 14:43
  • Post the class where you have the HashMap that you want to fill – Nadir May 02 '16 at 14:49
  • @MrunalGosar could you explain me with more detail, please – Bea May 02 '16 at 15:04
  • the class i want to put the hashmap only has the hasmap, nothing else @Nadir – Bea May 02 '16 at 15:05
  • @RC i cannot fill in the hashmap i created. If the hashmap is inside main i can fill it in but if i try to write the hashmap inside a class instead of main and fill it, when i'm trying to fill it in it says "package mapper does not exist" and "package furgo1 does not exit" when mapper is meant to be the hashmap name and furgo1 an object – Bea May 02 '16 at 15:11
  • As already asked, please edit your question and post the full non-working code. –  May 02 '16 at 15:37

1 Answers1

0

You could do like this:

Map<String, String> map = new HashMap<String, String>() {{
   put("1", "one");
   put("2", "two");
   put("3", "three");
}};

It is called "double brace initialization"

The first brace creates a new AnonymousInnerClass, the second declares an instance initializer block that is run when the anonymous inner class is instantiated.

Glib Martynenko
  • 128
  • 1
  • 9