Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error in subtracting 2 float values

Posted on 2004-10-11
7
Medium Priority
?
321 Views
Last Modified: 2010-03-31
Hello Everyone,

Following is the sample code that I want to run:

import java.lang.*;
public class Test{
  public static void main(String args[]) {
      String s1 = "5.5";
      String s2 = "8.5";
   
      Double i1 = Double.valueOf(s1);
      Double i2 = Double.valueOf(s2);

      double diff = i2-i1;
      if (diff>4)
            System.out.println("Test Successful...");
  }
}


Error that I am getting is:
"operator - cannot be applied to java.lang.Double".

I have to accept the string and then convert to double to perform the error checking comparison, thus, I cannot use compareTo() function. I have to perform the subtraction of the string that is converted to float. Can someone help me with it!!!

Thanks for any help.
0
Comment
Question by:seemasharma22
7 Comments
 

Accepted Solution

by:
sreejupamin earned 180 total points
ID: 12283991
You can say:
................................................
................................................
double diff=i2.doubleValue()-i1.doubleValue();
if(diff >4 )
....................


Hope this helped.

Regards,
nair
0
 
LVL 2

Expert Comment

by:gen718
ID: 12284002
try
   double diff = i2.doubleValue()-i1.doubleValue();
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12284007
You need to convert the values to double NOT Double.
i1 and i2 in ur code are objects, there is no way one object can be subtrated from another. First they have to be converted to primitives and then can be subtracted
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:thomas908
ID: 12284012
>>i1 and i2 in ur code are objects
I meant references to objects
0
 

Expert Comment

by:sreejupamin
ID: 12284154
Thank u, Seema.
0
 

Author Comment

by:seemasharma22
ID: 12284250
Thanks a Bunch Mr. Nair !!! :) your solution solved one of my biggest worry.
0
 

Expert Comment

by:sreejupamin
ID: 12284549
You are most welcome :))))))))))))))
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question