Results 1 to 3 of 3
  1. #1
    cela0811 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,000
    Rep Power
    10

    Default

    I'm not sure what your actual problem is. This seems to work fine for me:

    Java Code:
    public class Test{
    	public static void main(String[] args){
    		String stringD = "-1";
    		double doubleD = Double.parseDouble(stringD);
    		System.out.println(doubleD);
    	}
    }
    PS- Quote my message to see code tags in action.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    cela0811 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Byte value comes out negative
    By Bluefox815 in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 04:52 AM
  2. string to negative int problem
    By pendulum101 in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 06:47 PM
  3. I don't want negative money [BUG]
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 09:06 AM
  4. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:48 AM
  5. [SOLVED] Array of first negative number
    By random0munky in forum New To Java
    Replies: 9
    Last Post: 12-09-2008, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •