Hi! I am trying to write a method w/i a larger program, and it's purpose is to read in a section of the string between " " and then within that section split each subsection apart if it contains spaces and matches a particular RegEx and if a space is present, then the next letter after a spave MUST be capitalized. Anyway, I got all that to work as far as splitting the line and matching sections, EXCEPT the syntax on my print statements. I only want it to print out once if the string doesn't match or is valid, not for every subsection.
Example:
"John John" is a valid entry
But it prints:
VALID LASTNAME "John John"
VALID LASTNAME "John John"
"John John John John J" is invalid because the info btwn quotes is larger the 20 chars.
But it prints:
XXXXX LASTNAME "John John John John J", MALFORMED STRING
XXXXX LASTNAME "John John John John J", MALFORMED STRING
XXXXX LASTNAME "John John John John J", MALFORMED STRING
XXXXX LASTNAME "John John John John J", MALFORMED STRING
XXXXX LASTNAME "John John John John J", MALFORMED STRING
This is my code...how can I fix that loop? Thanks!
private static void checkLastname(String line, String field, String content) {
// TODO Auto-generated method stub
String subLine = line.split("\"\\s")[0]; //SPLITS LINE AT 2ND QUOTATION MARK
String field1 = field;
String[] subField = field.split("\\s");
for (int x=0; x<subField.length; x++){
if(field1.length()<1){
System.out.println("XXXXX " + subLine + "\"" + ", MALFORMED STRING");
}else if(field1.length()>20){
System.out.println("XXXXX " + subLine + "\"" + ", MALFORMED STRING");
}else if (!subField[x].matches("^[A-Z]+[a-z ]*$")){
System.out.println("XXXXX " + subLine + "\"" + ", MALFORMED STRING");
}
else{
System.out.println("VALID " + subLine + "\"");
}
}
}