[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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