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
Medium Priority
245 Views
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
Question by:LiquidMeth
• 3

LVL 8

Accepted Solution

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

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

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

ID: 9447373
LiquidMeth:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 8

Expert Comment

ID: 9450947
Points to: Exceter.
0

## Featured Post

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
###### Suggested Courses
Course of the Month10 days, 23 hours left to enroll