• Post Reply Bookmark Topic Watch Topic
  • New Topic

check a if a string is present in an integer type  RSS feed

 
Tersa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am writing a program that involves the user typing in an integer type value. However, i want to check if they type anything else, such as an string, so that i could display an error msg. How can i do this?? Thanks for your help
 
Rahul JG
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tersa
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain the code Rahul cuz i don't get it. thx
 
Rahul JG
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.parseInt("<input>") parses the string argument as a signed decimal integer.
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value.
It will throw a NumberFormatException if the string does not contain a parsable integer.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to check if they type anything else, such as an string, so that i could display an error msg. How can i do this??
It depends on the context of the application. Are you developing a web application, a console-based application, a SWING- or AWT-based GUI, or something else?
And another thing...
Tersa,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!