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: How to create an Int or double of what the user types int a JTextField?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create an Int or double of what the user types int a JTextField?

    I am not sure if this is the correct thread but it is what I believe is the closest to my question.

    --I am making a simple calculator and I need to make a double variable (or Int) that remembers what the user types inside the JTextField. How can I do this?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to create an Int or double of what the user types int a JTextField?

    You need to extract the text from the JTextField *when* you need it by calling getText() on the field, and then convert it to double or int (as the need may be) by using the double Double.parseDouble(String text) or int Integer.parseInt(String text) as the need may be.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: How to create an Int or double of what the user types int a JTextField?

    You might find it easiest to write your own class that has fields for both the string that the user entered and the int or double value it represents.

    It depends somewhat on what exactly you are trying to do. A simple calculator could get by with knowing the string (the sequence of digits) the user entered and, at some point, simply discard that information and replace it with a numerical value. In that case defining ypur own type (class) would be unnecessary.

Similar Threads

  1. [SOLVED] Reading in an int from a JTextField?
    By Cobalt in forum Object Oriented Programming
    Replies: 1
    Last Post: March 23rd, 2012, 07:59 AM
  2. JTextField returns a string, but I want an int
    By AceApple in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 10:52 PM
  3. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 10:57 AM
  4. Assigning an 'int' from a JTextField to a variable...
    By RiskyShenanigan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 26th, 2011, 05:00 PM
  5. Replies: 4
    Last Post: September 5th, 2010, 11:29 AM