Solved

Error in subtracting 2 float values

Posted on 2004-10-11
7
313 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now