?
Solved

Java Calculations

Posted on 2003-03-04
6
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

771 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