I am writing a Java program, and I need to be able to call these two methods, but I'm kind of confused.
These methods need to be able to match the following data:

This would be a sample input if what I am checking...

LASTNAME "Rosemont" #
LASTNAME "Rose Mont" #
USERNAME "jRose!" #
USERNAME "Rose55!" #

So LASTNAME or USERNAME would be the field, the stuff btwn "" is the content, and the # finishes off a valid entry...

checkLastname
A: Must start with an uppercase letter
B: Must only contain letters and spaces
C: Any letter after a space must be capitalized
D: Must be no longer than 20 characters
E: Must be at least one character

checkUsername
A: Must use at least half of the same unique characters as the last LASTNAME read in (you may assume that some LASTNAME appears before any USERNAME)
B: Cannot contain the character & twice consecutively
C: Cannot contain the character | twice consecutively
D: Must be at least five characters long, and no more than 15
E: Cannot contain the , character
F: Must be made up of either letters, numbers, or the following list of characters: !,@,#,$,%,^,&,*,(,),_,-,+,<,>,?,:

This is what I have so far...don't laugh!!!

private static void checkLastname(String line, String field, String content) {
		// TODO Auto-generated method stub
		{// last name
			if(field.equals("LASTNAME")){
				if(content.matches("[^[A-Z][A-Za-z ]*\\s*$]*")){
					System.out.println("VALID " + line);
					if(!content.matches("[^[A-Z][A-Za-z ]*\\s*$]*")){
						if(!content.matches("[^[A-Z][A-Za-z ]*\\s*$]*")){
							System.out.println("XXXXX " + line
									+ ", MALFORMED STRING");
						}
					}
				}

				System.out.println("XXXXX " + line
						+ ", UNRECOGNIZED FIELD");
			}
		}
	}// end Lastname
private static void checkUsername(String line, String field, String content) {
		// TODO Auto-generated method stub
		{ // Username 
			if (field.equals("USERNAME")){
				if (content.matches(" ")) {
					System.out.println("VALID " + line);
					//} else {
					if (!content.matches(" ")) {
						System.out.println("XXXXX " + line
								+ ", MALFORMED STRING");
					}
				}
				//} else {
				System.out.println("XXXXX " + line
						+ ", UNRECOGNIZED FIELD");
			}
		}

	}// end Username

So, I'm not sure how to get Lastname to be no more than 20 characters and also ensure that the next character after and space is capitalized, and so for Username to have to rely on Lastname is super confusing!!! Tips? Advice? Pleeease!!!

Recommended Answers

All 3 Replies

Why do we need to laugh at your code??? Anyway, you are on the right track using matches functionality. Though, your regExs aren't quite there yet. Also, you should rearrange the check because certain condition could be checked before others.

//E: Must be at least one character (which is supposed to be an uppercase)
//  the regex checks if there is at least one uppercase letter in there
//  Why do you need this one? The requirement "A" is superseded this requirement anyway.
content.matches("[A-Z]")
//A: Must start with an uppercase letter
content.match("^[A-Z]")
//B: Must only contain letters and spaces (no underscore or number allowed)
//  the regex is to check from start to end and return true if it contains
//  at least one alphabet/space
content.matches("^[A-Za-z ]+$")
//D: Must be no longer than 20 characters
if(content.length()<=20)
//C: Any letter after a space must be capitalized
// This one is a bit tricky. You can do it either using regex or manual manipulation.
// I am not sure you would understand how to use regex to do the checking, so
// I will explain how to do it manually.
// To do it manually, you could split the content using "\\s+" as delimiter. Then
// check each split content whether it starts with an uppercase using regex (as seen
// above).

For checking user name, it is more difficult and trickier. I also don't understand certain requirements...

//A: Must use at least half of the same unique characters as the last LASTNAME read in (you may assume that some LASTNAME appears before any USERNAME)
// Hmm... Not sure what this mean... Example please?
//B: Cannot contain the character & twice consecutively
// This as well... Example please?
//C: Cannot contain the character | twice consecutively
// This as well... Example please?
//D: Must be at least five characters long, and no more than 15
content.length()>=5 && content.length()<=15
//E: Cannot contain the , character
!content.matches(",")
//F: Must be made up of either letters, numbers, or the following list of characters: !,@,#,$,%,^,&,*,(,),_,-,+,<,>,?,: 
// Why do you need to check for "," if the whole content is supposed to contain only
// what in this list?
content.matches("^[\w!@#%&+<>:\\?\\-\\(\\)\\*\\$\\^]+$")

Holy Cow!!! You saved me!!! I felt like I knew what I wanted to do theoretically, but I was having trouble coding it properly. I actually did a drastic revision of my code on my own (no $40 an hour tutor, which led to the disaster above), and it works SO MUCH BETTER, and now with this final tip I can say I am on my way to being done!!! Thanks so much. I think tonight is the first night I have actually like writing code! Hahaha. Maybe I am not hopeless after all. :)

BTW, Hahaha..I wish I could tell you why we have to check all this craziness. I wish I could give you an example, but I am just as baffled. These specs for this lab are outta control!! I am just going to try and see if field.contains "&&" or something like that. If I make some progress and I have better examples, I will let you know! Other than that... thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.