Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

convert from string to int  RSS feed

 
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
 
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).
 
You got style baby! More than this tiny ad:
free 1 hour java lesson
https://coderanch.com/t/695988/free-hour-java-lesson
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!