简体   繁体   中英

Why won't my Java Map compile in IntelliJ?

I'm trying to create a Scrabble game using Java in IntelliJ. I decided to use a Map<Character, Integer> which will contain each letter tile and its corresponding scoring value. I figured I could use the ofEntries shorthand to initialize the values of the Map but each time I try to compile my program I get an error.

error: cannot find symbol Map.entry('A',1)  
symbol: method entry(char,int)  
location: interface Map  
26 errors (1 for each Map entry)

At first I thought it might have something to do with the Map being of type Character, Integer and the error mentioning entry(char,int) but it is my understanding that Java uses autoboxing between primitives and their object wrappers.

import java.util.Map;

class Scrabble {
    private final Map<Character, Integer> LETTER_SCORES = Map.ofEntries(
            Map.entry('A',1), Map.entry('E',1), Map.entry('I',1), Map.entry('O',1),
            Map.entry('U',1), Map.entry('L',1), Map.entry('N',1), Map.entry('R',1),
            Map.entry('S',1), Map.entry('T',1), Map.entry('D',2), Map.entry('G',2),
            Map.entry('B',3), Map.entry('C',3), Map.entry('M',3), Map.entry('P',3),
            Map.entry('F',4), Map.entry('H',4), Map.entry('V',4), Map.entry('W',4),
            Map.entry('Y',4), Map.entry('K',5), Map.entry('J',8), Map.entry('X',8),
            Map.entry('Q',10), Map.entry('Z',10)
    );

    private String word;
    private int wordScore;

    Scrabble(String word) {
        this.word = word;
    }

Please go to Project Structure | Project | Project SDK and define here JDK > 9 and Language level > 9. Also go to Project Structure | Modules and check that corresponding language level for module is defined here as well. Your code sample should compile without failures with JDK > 9 as was correctly mentioned in comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM