File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Selecting Proper Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Selecting Proper Number" Watch "Selecting Proper Number" New topic
Author

Selecting Proper Number

Rauf Khan
Greenhorn

Joined: Aug 11, 2010
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

Check out Mike Simmons' posts in this thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rauf Khan
Greenhorn

Joined: Aug 11, 2010
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

Joined: Aug 19, 2009
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.


OCPJP 6
Rauf Khan
Greenhorn

Joined: Aug 11, 2010
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
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

Joined: Aug 11, 2010
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

Joined: Aug 11, 2010
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

Joined: Aug 11, 2010
Posts: 10
This logic works to select the lower key from the TreeMap with Java 1.5

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Selecting Proper Number