2

How do i go about using compareto for a double and i want to turn it into an int?

An example would be nice. I have been searching the java api.

also is it possible to use if and else statements with a String toString method? Mine has kept on crashing for special cases.

im a total noob to java i guess i been reading constantly to learn

Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
Cferrel
  • 321
  • 2
  • 5
  • 19
  • 2
    You've got at least 3 different questions in there. Why don't you post some of the code you've been trying? – Matt Ball Apr 05 '11 at 20:40
  • im working on a project for school so i thought generic examples would be good since thats not cheating. I want to do my work honestly but examples are helpful when im searching for them. – Cferrel Apr 05 '11 at 20:50
  • That's a fair point, but you should at least phrase your questions more clearly and precisely. There's a difference between "general" and "vague and unclear." – Matt Ball Apr 05 '11 at 21:00
  • 1
    Let's say I were a professor (and this is just me, yours could differ in opinion) and I tasked four students with writing a large program to... simulate scheduling of students classes or something. If they asked someone "give me an algorithm for scheduling" I'd be inclined to think that's borderline cheating (although, saying "here's my algorithm, what do you think" might not be, it'd be case by case.) If they asked someone "how should I compare these two values" which is a very very small component of the system, I would say they're being resourceful. YMMV (And being students, YGMV!) – corsiKa Apr 06 '11 at 21:56

4 Answers4

3

If you want to compare an integer with a double you will need to convert the integer to a double. Please be aware that this won't work the other way round as an int couldn't hold all the values a double can: Values will be round down

double d = 12.3;
return (int)d;

will return 12! and doubles can hold values way bigger and small than int could

double d = 1.337E42
return (int)d;

returns 2147483647! There are many orders of magnitude in between. So please always convert the int to a double to prevent this to happen.

You can use following code for comparison:

int compare(double d, int i) {
   return new Double(d).compareTo(new Double(i));
}

But keep in mind that Double differs from double as Double is an object and not a primitive type, so there will be more overhead when handling a Double compared to using a double or int.

As for the second part of your question I don't really understand what's your question/intention. Please try to clarify it.

TheMorph
  • 572
  • 2
  • 10
1

you can try something like this

    Double d = 10.0;
    Integer i = 10;
    d.compareTo(i.doubleValue());

or

    i.compareTo(d.intValue());

I'm not sure which one you need.

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • The latter one will not work correctly. Insert d = 10.3 and i = 10. What happens is, that d.intValue() will return 10. So you are effectively comparing now 10 with 10 and not with 10.3 as intended. – TheMorph Apr 06 '11 at 20:33
  • @TheMorph True but there are cases when you might to do the latter and from reading the question as it was posted, I couldn't tell what the OP's intention with this statement `compareto for a double and i want to turn it into an int` – Bala R Apr 06 '11 at 20:39
1

You do not want to convert to String because that would compare the numbers lexicographically rather than numerically.

I will take your question to mean, "How do I create a compareTo() method for doubles?"

I also think you are using this to implement a data structure for doubles.

Here is how I would go about it. When you construct a data structure, you will construct it as a Double object. Double is a built-in class in java.lang package that boxes the double primitive.

Then, java will automatically cast them to that type.

The reason you want to use the Double class as the defining type for your data structure is so that you can use its built-in compareTo method. There is no way to make the primitive data type double contain the compareTo method.

Here is some code to help you get started for example:

TreeMap <Double, String> myDoubleMap = new TreeMap <Double, String> (10);
for (int i = 0; i < 10; i++)
    myDoubleMap.put ( Math.sqrt( 10.0 * i) , "" + i);


System.out.println(myDoubleMap);

(Make sure you import java.util.TreeMap if you are to run this example)

Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
  • Why is a TreeMap even involved in this? – bojangle Apr 05 '11 at 23:56
  • I thought that the OP was trying to figure out how to store `double` primitive datatypes in data structures. The error that is indicated for this is that `double` does not contain a `compareTo()` method. I indicated in my post that storing the primitive data type in the structure is not possible. I used TreeMap as a simple example to indicate that the `TreeMap` data structure (which requires the `compareTo()` method) to show how to add primitive types to data structures using the concept of autoboxing. – Kaushik Shankar Apr 23 '11 at 22:52
0

I can only guess at your exact requirements. You should try to refine them a little more clearly.

If you want to compare doubles and ints, it may be tough. You have two options.

one: cast the double to an int, and compare

int compare(double d, int i) {
    int dint = (int)d;
    return Integer.compare(dint,i);
}

int compare(int i, double d) {
    int dint = (int)d;
    return Integer.compare(i,dint);
}

two: cast the int to a double, and compare

int compare(double d, int i) {
    double idouble = (double)i;
    return Double.compare(d,idouble);
}

int compare(int i, double d) {
    double idouble = (double)i;
    return Double.compare(d,idouble);
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • Do not convert an double into an int, it can and will break as you loose precision and moreover double can hold values way bigger and smaller than int could. Or explained otherwise: an double won't fit into an int. Following an sample output: `d: 1.337E42, (int)d: 2147483647, d == (int)d: false` – TheMorph Apr 06 '11 at 20:25
  • Interesting - I would have expected `(int)1e40` to throw an exception, not silently cap at `MAX_VALUE`. This is, however, easily caught by a check on the double value. – corsiKa Apr 06 '11 at 20:47
  • In any event, this is at best a guess as the requirements are extremely vague. – corsiKa Apr 06 '11 at 20:49
  • I was not sure what would happen so I tried it. This was the result. ;) As for your check: Yes it can be done, but i find it better to understand if there are no checks needed, that could be avoided. – TheMorph Apr 06 '11 at 21:20
  • But even if there would be an exception thrown for values too big or small for int: casting double d = 10.3 to an int would return 10, breaking the comparison for another reason as double d = 10.3 would seem equal to int i = 10. – TheMorph Apr 06 '11 at 21:37
  • Our goal is to compare the two, but the definition of the comparison isn't clear, so we can't decide what is better. Should `compare((double)10.3,10) == 0`? We don't know. As for "checks should be avoided" why do you say that? Explicit logic is always better for code clarity than implicit logic, and the cost of the check is trivial. – corsiKa Apr 06 '11 at 21:53
  • As I agree with you, that explicit logic is better than something "magically is happening behind the scenes" what might be misunderstood or break easily i have to disagree with you in 2 other points. First: As we don't know what is really needed I think we should imply the common and more general case. Second: Just by converting the int to double we don't need any checks, neither explicit nor implicit. I for myself like this approach as it really avoids the corner cases one would have to consider. Less to consider, less possibilities for errors. – TheMorph Apr 06 '11 at 22:01