Results 1 to 3 of 3
Thread: Help with negative numbers
- 01-04-2011, 04:45 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Help with negative numbers
I'm writing a calculator in Java and I am having trouble with negative numbers. The numbers are stored in and array of strings, called input, and Output is a text field where I display my results. Temp is a double. When I run the program and enter a negative number, it acts as if it is positive. I convert a string to a double, multiply by -1, and convert back to a string to display. What is wrong? Here is the relevant code:
{
temp = Double.parseDouble(input[2]);
temp = -1 * temp;
Output.setText(temp.toString());
}
P.S. Is there a special way to format code? If so, what is it?
- 01-04-2011, 04:47 PM #2
I'm not sure what your actual problem is. This seems to work fine for me:
PS- Quote my message to see code tags in action.Java Code:public class Test{ public static void main(String[] args){ String stringD = "-1"; double doubleD = Double.parseDouble(stringD); System.out.println(doubleD); } }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-04-2011, 05:04 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
I fixed it. It turns out that while I updated the display, I didn't store the new value in my input array. I have another class which takes the input and finds the result, but it never got the new value. That was stupid of me.
Java Code:public class Main public static void main(String args[]) { System.out.println("Thanks for telling me about the code tags."); }
Similar Threads
-
Byte value comes out negative
By Bluefox815 in forum New To JavaReplies: 1Last Post: 07-03-2010, 03:52 AM -
string to negative int problem
By pendulum101 in forum New To JavaReplies: 5Last Post: 11-01-2009, 05:47 PM -
I don't want negative money [BUG]
By anthonym2121 in forum New To JavaReplies: 1Last Post: 04-07-2009, 08:06 AM -
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM -
[SOLVED] Array of first negative number
By random0munky in forum New To JavaReplies: 9Last Post: 12-08-2008, 11:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks