My Java application is displaying some odd behaviour, and I'm having trouble finding a solution.
This code snippet takes the contents of a LinkedHashMap
where the keys and values are both of type String
and writes them to a text file. configWriter
is of type PrintWriter
.
for (Map.Entry<String, String> entry : configMap.entireSet()) {
configWriter.println(entry.getKey() + "=" + entry.getValue());
}
The map was declared:
LinkedHashMap<String, String> configMap = new LinkedHashMap<String, String>();
It is populated by reading tokens from a text file using the Scanner
class.
One of the values in the map is a String
that contains only numbers (an integer), but when the loop gets to this value, it throws a ClassCastException
saying that you cannot cast from Integer
to String
. This makes perfect sense, but the type of the values is String.
Is there a way to force Java to keep this value as a String
?