I have user enter in a first name. how do I make sure that the first name is at least 2 characters long and only alphanumeric?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have user enter in a first name. how do I make sure that the first name is at least 2 characters long and only alphanumeric?
Take a look at the API for the String class...it has many methods to help accomplish what you want.
In case anyone is wondering how I accomplished it (this is using methods/objects) it turns out that I needed to just default names to First and Last not re-submit data.
public void setfirstName(String first){ if (first == "" || first.length() < 2) this.firstName = "First"; else this.firstName = toProperCase(first); }
public void setlastName(String last){ if (last == "" || last.length() < 2) this.lastName = "Last"; else this.lastName = toProperCase(last); }
public static String toProperCase(String data){ String firstLetter = data.substring(0,1).toUpperCase(); String restLetters = data.substring(1).toLowerCase(); return firstLetter + restLetters; }