This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

User Input check  RSS feed

 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to to check, whether the input is a number.

eg: telephone number
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few ways to do it...

The Character class has a method to check if a individual character is a digit. You can break your input string into characters and check each character to see if it forms a the digits for the number.

If you feel comfortable with Regular Expressions, you can use the strings matches() method to see if it is a number... like so...

input.matches("\\d+");

Of course, if this is a homework problem, then you probably are expected to check each character directly -- depending on the restrictions of your assignment.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, didn't see the reference to "telephone" number. The regular expression that I provided was for any number, but you probably already figured that out.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!