Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

convert from string to int

 
Henk Rog
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have made an application with a textfield. The value entered in the textfield
should be greater than a certain int. How can I do that? I get an error:
can't convert from string to int.
thanks
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To limit the text field to numeric input, use the NUMERIC constraint in its constructor.
To convert its value to an integer, use method Integer.parseInt(String).
To check each value the user enters, add an ItemStateListener to the text field's enclosing Form, and in the itemStateChanged method, check if it's the text field that changed, and if so, whether the new value is OK. If not, display an Alert, and probably set the text field back to the preceding valid value. (Note: I've not used ItemStateListener myself - this is just from reading the JavaDocs).
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!