Bank Account Management Program (Part 2)

+1 Arnab Banerjee · February 13, 2015
I want to make sure that user does not enter any numbers or special characters while entering their name, how to do that.. ???
void setAccount(){
System.out.println("Enter user's first name ");
fn = x.nextLine();
System.out.println("Enter user's second name ");
sn = x.nextLine();


Post a Reply

Replies

Oldest  Newest  Rating
+2 Steve O · February 13, 2015
There's a detailed answer on this page: http://stackoverflow.com/questions/12831719/fastest-way-to-check-a-string-is-alphanumeric-in-java

For instance:


boolean isAlphanumeric(String str) {
   for (int i=0; i<str.length(); i++) {
       char c = str.chatAt(i);
       if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a)
           return false;
   }

   return true;
}


In the above snippet, I think (hope) I'm right in saying that it's important that char c is tested against the Unicode character set (and not the ASCII character set). You can see this above with the operands in hexa[adecimal] format, i.e. "0x3a", "0x40", etc.

HTH
Steve
  • 1

Java / Android Development

107,325 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator