Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pj6444

Thread: calculations on text box inputs

  1. #1
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default calculations on text box inputs

    Hi guys,

    I have made a simple form, it consists of 3 text box's textbox1, textbox2 and textbox3 as well as 1 button button1. What I want to be able to do is put numbers into textbox 1 and 2 and then show the multiplication of these numbers in textbox 3 when the button is pressed and I was wondering what is the standard way of reading these numbers from the text box and allowing us to do the conversion obviously in the textbox its a string and we need to convert to an int or double or whatever to be able to perform calculations on them then convert it back into a string to display in text box 3?

    Any help would be greatly appreciated. Thanks.
    Last edited by Dark Knight; 03-07-2014 at 12:16 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,922
    Rep Power
    5

    Default Re: calculations on text box inputs

    You don't read the numbers. They are just typed in the text box (I assume JTextField). They can be retrieved with the getText() method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Re: calculations on text box inputs

    Thanks for the input but I cant do textBox3.setText(textBox1.getText() * textBox2.getText());

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: calculations on text box inputs

    The thing that you need to do is convert those both into numbers(ints, doubles, floats, longs). When you use the getText() method it returns a String which you can not use operators on. You need to convert the two Strings to numbers, multiply them and then set the text to that number in String form.

    So what you could do is:

    Java Code:
    textbox3.setText("" + (Integer.parseInt(textBox1.getText()) * Integer.parseInt(textBox2.getText())));
    Last edited by pj6444; 03-07-2014 at 01:03 AM. Reason: Adding example code
    Dark Knight likes this.

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2012, 01:21 PM
  2. Replies: 3
    Last Post: 09-06-2012, 06:30 PM
  3. OO for calculations?
    By StateMachine in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 09:17 AM
  4. update text output (eg. jLabel) during calculations
    By kosmo76 in forum AWT / Swing
    Replies: 2
    Last Post: 04-06-2011, 11:30 PM
  5. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 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
  •