0

I am getting an error that says... "put(Object,Object) in UIManager cannot be applied to (int) "

    Random rand = new Random();

    int randomNum = rand.nextInt(7)+1;

    Map <String, Integer> colours = new HashMap<>(); {{
       put("red",1);
       put("white",2);
       put("purple",3);
       put("orange",4);
       put("pink",5);
       put("yellow",6);
       put("green",7);
       put("blue",8);
    }};

    Map <String, Integer> answer = new HashMap<>(); {{
        put(randomNum);
        put(randomNum);
        put(randomNum);
        put(randomNum);
    }};

I'm trying to set a map full of colours, then apply 4 random colours to a new map. It'll allow any colour etc pink pink pink pink, red yellow white white... Where am I going wrong with this?

Tyler
  • 191
  • 17
  • Possible duplicate of [How to directly initialize a HashMap (in a literal way)?](https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way) – takendarkk Jan 10 '18 at 15:07
  • 1
    You are getting that error because `Map::put` takes two parameters, not just one. Read the [documentation](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V-) please – smac89 Jan 10 '18 at 15:25

2 Answers2

1

I think your design is not that correct. Maybe you should do something like that :

    Random rand = new Random();

    Map<Integer, String> colours = new HashMap<>();
    colours.put(1, "red");
    colours.put(2, "white");
    colours.put(3, "purple");
    colours.put(4, "orange");
    colours.put(5, "pink");
    colours.put(6, "yellow");
    colours.put(7, "green");
    colours.put(8, "blue");

    List<String> answer = new ArrayList<String>();
    answer.add(colours.get(rand.nextInt(7) + 1));
    answer.add(colours.get(rand.nextInt(7) + 1));
    answer.add(colours.get(rand.nextInt(7) + 1));
    answer.add(colours.get(rand.nextInt(7) + 1));
anL
  • 1,073
  • 1
  • 11
  • 31
0

You are putting only integer, which must be in reference like <String, Integer> Map format:

Map <String, Integer> answer = new HashMap<>();   {{
         put(randomNum);
         put(randomNum);
         put(randomNum);
         put(randomNum); 
}};

try like this:

Random rand = new Random();

Map <String, Integer> colours = new HashMap<>();
colours.put("red", 1);
colours.put("white", 2);
colours.put("purple", 3);
colours.put("orange", 4);
colours.put("pink", 5);
colours.put("yellow", 6);
colours.put("green", 7);
colours.put("blue", 8);

//creating a Set variable for colours keySet
Set<String> keySet = colours.keySet();

//creating a list variable for getting the key by putting value
ArrayList<String> keyList = new ArrayList<String>(keySet);

Map <String, Integer> answer = new HashMap<>();

int randomNum = rand.nextInt(7)+1;
for(int i = 1; i <= 8; i++){

    //creating a random number between 1 to 7
    randomNum = rand.nextInt(7)+1;

    //first getting the key by value and putting in map as key
    answer.put(keyList.get(randomNum), randomNum);

    //showing the mapped value as key color name 
    System.out.println(answer.get(keyList.get(randomNum)));
}

Please add a String object there as a key, to remove the error.

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48