• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Selecting Proper Number

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Mike Simmons' posts in this thread.
 
Rauf Khan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This logic works to select the lower key from the TreeMap with Java 1.5

 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic