Solved

Error in subtracting 2 float values

Posted on 2004-10-11
7
315 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 45 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 47
Oracle SQL syntax check  without executing 6 54
Opening PDF on button click and fill new document 2 37
null output 3 25
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 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