Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Selecting Proper Number

Rauf Khan
Greenhorn
Posts: 10
Hi,

Can you please help me to select the score value which must be rounded down to the nearest score if its not found.

I need to write a method taking score as input and return its value, if the value is not found need to return the nearest value
i.e suppose if the input value is 0.49 then its needs to return 80.
suppose if the input value is 0.94534 then we dont have the value in the map. So then since the input is <1 but >0.72 so it should consider 0.72 as input and return 90.

Thanks in Advance
Khan

Rob Spoor
Sheriff
Posts: 20667
65
Check out Mike Simmons' posts in this thread.

Rauf Khan
Greenhorn
Posts: 10
Rob, thanks for the response.

That thread is somthing different. Its just acting like a counter.

My problem is:

Suppose if I pass 0.49 to method displayRating() then its should display 80
suppose if I pass 0.94534 to method displayRating() then its should display 90, because we dont have the value in the map for 0.94534. So it should consider a value 0.94534<1 but 0.94534>0.72

Vinoth Kumar Kannan
Ranch Hand
Posts: 276
I'd suggest you convert the keySet of your map to a TreeSet and then apply floor() or lower() [Note : both are different] on it and narrow down on a particular key.
There you go - fetch the corresponding value from the Map.

See if this works.

Rauf Khan
Greenhorn
Posts: 10
Its throwing the below exception:
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap\$KeySet cannot be cast to java.util.TreeSet

Instead of HashMap if its a TreeMap then I can use the "lowerKey" function to get the nearest value.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Somewhere, in code that you didn't show us, you are casting the keySet of a hash map to a TreeSet. That is not going to work. I guess you did that because of Vinoth's suggestion. But converting the keySet to a TreeSet is not the same as casting it. In fact, casting something doesn't do any kind of conversion at all. It just tells the compiler "I have this object here, you should treat it as if it is a TreeSet". If then, at runtime, the object turns out not to be a TreeSet, you'll get a ClassCastException.

What you could do is use a TreeMap instead of a HashMap. Class TreeMap contains the lowerKey method that you're looking for.

By the way, using floating-point types such as Double as the keys in a map is not a good idea. Floating-point types are inherently imprecise, and maps are really made to lookup things with identifiable, exact keys.

Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Actually, what I meant by converting the keySet into a TreeSet is that, use the constructor of TreeSet that takes in a collection & pass the keySet to it.
Ahh..yes...I forgot TreeMap has such methods too...as it implements NavigableMap.
Rauf, as Jesper says, you can go directly for a TreeMap, rather than taking the keySet and converting it to a TreeSet..blah blah..

Rauf Khan
Greenhorn
Posts: 10
Yes, finally I did with TreeMap only. I have mentioned in the above post.

Thanks a lot 2 everyone for looking into this issue.

Rauf Khan
Greenhorn
Posts: 10
Hi,

Can anyone please tell me how to do the same kind of check w.r.t Java 1.5 as lowerKey method is part of 1.6, as my application supports only 1.5

Thanks in Advance
Khan

Rauf Khan
Greenhorn
Posts: 10
This logic works to select the lower key from the TreeMap with Java 1.5

Mike Simmons
Ranch Hand
Posts: 3090
14
Or you could just use

Optionally you can add a call to Integer.valueOf() in there. From your code, it's unclear if you would need this or not; your usage is inconsistent. You should probably sort that out.

 Don't get me started about those stupid light bulbs.