Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Error in subtracting 2 float values

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
seemasharma22
Asked:
seemasharma22
1 Solution
 
sreejupaminCommented:
You can say:
................................................
................................................
double diff=i2.doubleValue()-i1.doubleValue();
if(diff >4 )
....................


Hope this helped.

Regards,
nair
0
 
gen718Commented:
try
   double diff = i2.doubleValue()-i1.doubleValue();
0
 
thomas908Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
thomas908Commented:
>>i1 and i2 in ur code are objects
I meant references to objects
0
 
sreejupaminCommented:
Thank u, Seema.
0
 
seemasharma22Author Commented:
Thanks a Bunch Mr. Nair !!! :) your solution solved one of my biggest worry.
0
 
sreejupaminCommented:
You are most welcome :))))))))))))))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now