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 + "\"");
			}
		}

	}

The problem is that you print out a message on every single check of your sub string from split(). What you may want to do is to create a boolean variable outside the loop and set it to true. Then inside the loop, if there is anything that cause the string you are checking to be false, reassign the boolean to false and break from the loop. Now, after the loop, check if the boolean is still true. If it is true, it is valid and display a message, otherwise, display that it is invalid. Oh and you need to get rid of the "else" condition in line 20~22 because you don't need it.

Edited 5 Years Ago by Taywin: n/a

Well... Don't use " (double quotes) to demark your substrings!!!
You are forgetting that the delineators for a String IS the " (double quotes).
This will make things REALLY confusing.

The problem is that you print out a message on every single check of your sub string from split(). What you may want to do is to create a boolean variable outside the loop and set it to true. Then inside the loop, if there is anything that cause the string you are checking to be false, reassign the boolean to false and break from the loop. Now, after the loop, check if the boolean is still true. If it is true, it is valid and display a message, otherwise, display that it is invalid. Oh and you need to get rid of the "else" condition in line 20~22 because you don't need it.

I know that I was doing that, but I was just confused on how to NOt do it, haha. I've only been programming for a few weeks (well, Java), so I get easily confused. I will try your suggestion out. I am not sure, honestly how to do this, but I will give it a shot and see how it goes! Thanks!

Well... Don't use " (double quotes) to demark your substrings!!!
You are forgetting that the delineators for a String IS the " (double quotes).
This will make things REALLY confusing.

What do you mean? Sorry, what you posted is confusing to me. I have to read in the file designated by my instructor, so I'm not sure where to break the line other than the quotes. The full input is

NAME "John Smith" #

amongst many other things. What I need to find out is if the name between the quotes is valid according to our lab specs. If the name was

"John smith" it would be invalid b/c of the 2nd mane not being capitalized and I would output:

XXXXX NAME "John Smith" # MALFORMED STRING

but I am printing too many statements. Does that make sense?

The file given to you IS the file given to you.
So... Ignore my previous post. (Sorry!)

You have a LOT of stuff going on here.
Why do you have 3 arguments? I would think that only 1 would suffice.
You have 2 split()s. I would think that only 1 would suffice.
Are you sure that you are checking the length of the name and not the entire line?
Also, Why do you need a loop if you are only checking 1 line at a time?
Organize your code and keep it simple.
Build it step by step, and put a lot of println()s so you can keep track of what is happening.
Good Luck!

Edited 5 Years Ago by hfx642: n/a

This article has been dead for over six months. Start a new discussion instead.