• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

TEXTFIELDS TO INT

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone
How do you make a textfield read as an int?
I know it has something to do with Integer.parseInt()
but do not know how it works.
I have six TextFields, named textField1, textField2, textField3 etc... and only 2 digits can be inserted.

TextField textField1 = new TextField(2);
TextField textField2 = new TextField(2);
etc...
but how do I make int an int???
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = Integer.parseInt(texfield1.getText());
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you thomas
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you were doing this for six textFields, int i would not work for all of them, so , what could be done to solve this.?

???
can something bedone to make this more simple?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a reason that all the ints must be stored in the same variable? Perhaps creating a separate variable to hold each result might be a good idea?
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well each text field id going to insert the users lottery numbers.The user can insert 6 numbers. and then once this is done when pressed ok this will let the computer generate its own random numbers.
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have added the Integer.parseInt within the textfields, it compiles successfully, but when I execute, the Applet does not initialise.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you get any exceptions when you run it? What does it do?
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I ran it with the textField changed to an int, it did not do anything, not even show my designed class.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rupinder
Where in the code did you add the parseInt calls? Are in an event handler or in some other part of the code? Can you show the code where it is added - if it's a lot of code just show us a snippet and let us know where it falls in the whole thing.
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Rupinder
I was just wondering, the way I understand this is the user must enter numbers into these TextFields and then click on the ok button, but why are you then adding action listener to each TextField ? if the code in actionperformed() should only be executed once the user presses the ok button this will never happen. because you have no action listener on the button , and from what I can see, it will execute for each key pressed in any of the Fields.
[ August 27, 2002: Message edited by: Thomas Paul ]
 
Rupinder Bains
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SO how do I attempt to make the button work with textFields1,2,3,4,5 and 6.
How do I make it like make the random generator work once button ok has been pressed.
I need it to generate and activate the textfields 7, 8, 9 ,10 ,11 and 12.>
So once ok is pressed it will automatically do this.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to add a listener to the button press and then process the text fields when the button is pressed. You don't need to listen for any events on the text fields.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic