Reputation: 173
I got a problem even my professor couldn't solve after an hour of investigation: I have a map which stores a highscore value for each level, where the levels are saved as strings and the integer represents the highscore. Now, when I try to read the highscore for a level, I get this very weird problem: Upon calling the method to read the highscore, I get an error saying
java.lang.String cannot be cast to java.lang.Integer
The code is the following
public Map<String, Integer> highscores = new HashMap<>();
highscores.put("Level1", 35); //Example, we read it from a file
int highscore = highscores.get("Level1");
The error occurs in the third line. Does anyone have an idea why this happens? Integer.parseInt doesn't work either, as that method says it needs a String instead of an Integer as an argument, meaning the right hand side of the line is in fact an Integer. Any help is greatly appreciated.
Upvotes: 1
Views: 2096
Reputation: 173
I just found the answer in the reading of the file, no idea how I overlooked this one! It said
highscores = gson.fromJson(new BufferedReader(
new FileReader("ressources/highscores.json")),
new TypeToken<Map<String, String>>() {}.getType());
Which obviously made the value of the Map a String. Changing the second argument to Map<String, Integer>
solves the problem. Thank you for the very fast responses!
Upvotes: 1
Reputation: 1
Seems the code that you have mentioned here is perfectly fine, this problem is most likely occurring when you are reading from a file.
Are you sure you are converting whatever you read from the file into an Integer
before doing a put()
into the Map
?
May be you are trying to do something like this:
public class Test {
public static Map<String, Integer> highscores = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream("input.txt")));
String line = null;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
highscores.put(tokens[0], Integer.parseInt(tokens[1]));
}
System.out.println(highscores.get("Level1"));
System.out.println(highscores.get("Level2"));
}
}
Level1,35
Level2,65
35
65
Upvotes: 0
Reputation: 999
You should determine the element of highscores
type.Like following code:
System.out.println(highscores.get("Level1").getClass());
In my guess,the result of above code is not class java.lang.Integer
,if the result is Class java.lang.String
you can modify your code to following:
Map<String, Integer> highscores = new HashMap<>();
highscores.put("Level1", 35); //Example, we read it from a file
Object item=highscores.get("Level1");
if(item.getClass().equals(String.class)) {
highscore= Integer.parseInt((String) item);
}else if(item.getClass().equals(Integer.class)){
highscore=(Integer)item;
}
Upvotes: 0
Reputation: 446
When you say that you read it from a file, are you sure that it is an Integer and not a String?
The line of code
int highscore = highscores.get("Level1");
does two things and is similar to the following:
Integer highscoreObj = highscores.get("Level1");
int highscore = highscoreObj.intValue();
What happens when you try this one?
Upvotes: 1
Reputation: 1087
Sounds like when you're reading the score from a file, it's reading it as a string and then trying to stick it into the HashMap as a String
object instead of an Integer
object. That's where this exception comes from:
java.lang.String cannot be cast to java.lang.Integer
Upvotes: 1