programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Selecting Proper Number

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Hi,

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.

Khan

Sheriff
Posts: 22701
129
• Number of slices to send:
Optional 'thank-you' note:
Check out Mike Simmons' posts in this thread.

Rauf Khan
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 276
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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

Khan

Rauf Khan
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
This logic works to select the lower key from the TreeMap with Java 1.5

Rancher
Posts: 4334
59
• Number of slices to send:
Optional 'thank-you' note:
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.

 Whip out those weird instruments of science and probe away! I think it's a tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising