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 offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

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

Posting Permissions

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