Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: newbie jtextfield question

  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default newbie jtextfield question

    so, i'm trying for a user to enter number in jtextfields and then when they press jbutton to add...the program takes the 2 numbers entered and adds them up....my program actually somewhat works but i'm converting from text to double and then back to text to be able to print the number in jlabel. I need your help in simplifying the code please. TKS.

    public void actionPerformed(ActionEvent ae)
    {
    String text1 = one.getText();
    double aDouble = Double.parseDouble(text1);
    String text2 = two.getText();
    double bDouble = Double.parseDouble(text2);
    double answer = aDouble + bDouble;
    String aString = Double.toString(answer);
    testing.setText(aString);
    }
    });

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: newbie jtextfield question

    need your help in simplifying the code
    What needs to be simplified?
    It looks simple to me. Any changes would make it more complicated.
    You have one statement for each step that needs to be taken.
    camickr likes this.

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: newbie jtextfield question

    Ok. cool. also, If the user enters something that's not a double/int I need to have it set to 0 in that case. I looked and can't find anything that will do that for me. How can i check if the user actually entered a double and if he/she didn't, use the value of 0?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: newbie jtextfield question

    parseDouble() will throw an exception if the string argument cannot be parsed as a double. Catch the exception, and set the value equal to zero in that case.

    Double (Java Platform SE 6)
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

  5. #5
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: newbie jtextfield question

    Ok. Tks. I found that online, thought there would be an easier way.....Let me try this and see what happens....TKS FOR THE HELP

  6. #6
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: newbie jtextfield question

    Instead of catching exceptions, can i use a scanner let's say to identify if the input is a double? Wouldn't that be a more effective way of doing it?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: newbie jtextfield question

    Are you suggesting that You would take the String from the text field, create another object with the String and then ask that object if the data it has is a valid double?

    What do you mean by "more effective"?

  8. #8
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: newbie jtextfield question

    yeah, you're right.......I read the exceptions stuff and input in my program and works like a charm....thanks man...

    Java Code:
     public void actionPerformed(ActionEvent ae)
            {
               
                String text1 = one.getText();
                String text2 = two.getText();
                try {
                aDouble = Double.parseDouble(text1);
            } catch (NumberFormatException e)
            {
                aDouble = 0;
            }
            try {
                bDouble = Double.parseDouble(text2);
            } catch (NumberFormatException e)
            {
                bDouble = 0;
            }

Similar Threads

  1. Public JTextField question
    By Someone001 in forum AWT / Swing
    Replies: 5
    Last Post: 10-26-2011, 02:45 PM
  2. JTextfield question
    By xyknight in forum New To Java
    Replies: 3
    Last Post: 04-08-2011, 04:25 PM
  3. JTextField Question
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 12:38 AM
  4. JTextField Question
    By Kyle227 in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 07:44 AM
  5. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 02:39 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
  •