Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Calculations

Posted on 2003-03-04
6
Medium Priority
?
245 Views
Last Modified: 2010-04-17
I am curious as to why when I try to multiple my number by a negative to return a negative result all the code seems to do is return a positive number adding the .0 to the end

Example

I type in 8 and press the negative sign. It then returns 8.0 instead of -8.0

This is the code that has been written.

else if ("Neg" == ae.getActionCommand())
     {
     double dblTemp;
     dblTemp = (-1) * Double.parseDouble(display.getText());
     display.setText(Double.toString(dblTemp));
     }
0
Comment
Question by:LiquidMeth
  • 3
5 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 8067204
Your code is fine. That will return a negative number provided that you entered a positive one.

Are you sure that this code is being called?

>> else if ("Neg" == ae.getActionCommand())

Try changing this to,

if( ae.getSource() == display )

Then type in 8 and press enter. You should get -8.0 as the result.

Exceter
0
 

Expert Comment

by:EVelasco
ID: 8068948
"I type in 8 and press the negative sign. It then returns 8.0 instead of -8.0"

if you press 8 and then -, you are entering:  8-
you need to be entering negative first, then 8:   -8
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8068991
>> if you press 8 and then -, you are entering:  8-

This would simply generate a NumberFormatException.

>> you need to be entering negative first, then 8:   -8

That would return a positive number anyway because the code LiquidMeth posted multiplys the value found in display by -1.

-1 * -8 = 8

Exceter
0
 

Expert Comment

by:CleanupPing
ID: 9447373
LiquidMeth:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9450947
Points to: Exceter.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

571 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