Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?

    I used the following code to check whether a input number is negative or not.
    But it's not working.
    Can anyone please help me?

    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    private JTextField theLength; //a type of JTextField
    private JLabel theLabel //a type of JLabel

    public static double stringToDouble(String stringObject)
    {
    return Double.parseDouble(stringObject.trim());
    }


    public void actionPerformed(ActionEvent e)
    {
    if ((stringToDouble(theLength.getText())) < 0)
    {
    theLabel.setText("the number is negative.");
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    What happens when the code is executed? Add some println statements to print out the values of the variables that are used and given values. For example: what String is passed to the stringToDouble() method? What value does the stringToDouble() method return?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hi, how can I check whether a number that is transformed by a wrapper class is negative or not in Java?

    Also posted at Hi, how can I check whether a number that is transformed by a wrapper class is negati - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. wrapper class
    By mysterious.ek in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2012, 11:27 AM
  2. number turning negative during calculation why???
    By derekxec in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 3rd, 2012, 11:35 AM
  3. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  4. the definition of a wrapper class
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:54 AM
  5. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM