1

Hi I am having an error running this code

Map<String, Integer> map = new TreeMap<String, Integer>();//Already populated public ArrayList<Map<String, Integer>> storage(){ for (String getCharacter : map.keySet()){ String convertBinGrp; **Integer binGroups = Integer.valueOf(getCharacter);** if ( binGroups>=0 && binGroups<= 32){ convertBinGrp = Long.toString(binGroups); binInsideList.get(convertBinGrp); Integer getVal = getCharacter.getValue(); binInsideList.put(convertBinGrp, getVal); } } 

error message: Exception in thread "main" java.lang.NumberFormatException: For input string: " "
The error indicated is the Integer binGroups = Integer . valueof(getCharacter) casting. I've already tried using long and double just to make sure its size is bigger because I understand its a String value taken from Map. But is there any other way to solve this? I wanted to convert the String into a numerical value so that I can put it into proper bins or groups. Thanks

2 Answers 2

5

Your map contains " " as one of the keys. The code is trying to interpret the " " as a number, yet it's not a valid number.

You need to figure out where the space is coming from, and then decide on the best course of action (fix the problem that caused it, ignore it, etc).

Sign up to request clarification or add additional context in comments.

Comments

2

The Exception is correct: Integer.valueOf(" ") will give you java.lang.NumberFormatException. The initialization (population) of your TreeMap is giving you values that are whitespaces.

Try:

System.out.println("Key is: '" + getCharacter + "'); 

before Integer.valueOf(getCharacter); And seek error in population if there is "one white-character key"

1 Comment

Hi tnx for replying yeah I tried ur approach as well. It was my mistake I thought that all the values in Map (K part) are all numbers. But I already solved it. Tnx for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.