Oh Pie
Oh Pie

Reputation: 23

Input String with if doesn't work

I have following problem:

The user has to put his name correctly in my program:

  1. the name only contain letters, hyphens "-" and blanks " "
  2. the first letter should be a capital letter.
  3. after a blank or hyphens should be followed by a capital letter.

For example only this form should be accepted by the program:

"Name" or "Firstname-Secondname" or "Firstname Secondname".

The 3rd point doesn't work in my code:(

My Java code:

public class Test {
    private static Scanner scanner = new Scanner(System.in);
    private static String name;

    public static void main(String[] args) {

        boolean check = false;

        check = checkName();
        System.out.println("Check= "+check);
        output(check);

    }

        public static void output(boolean check) {
            if (check == false) {
                System.out.println("Fehler");
            }   
            if(check == true) {
                System.out.println("Dein Name ist: "+name);
            } 
        }//End output()

    public static boolean checkName() {

        System.out.print("Name: ");
        name = scanner.nextLine();
        boolean check = false;

        if(name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') {

            for(int i=1; i < name.length(); i++) {
                if (name.charAt(i) >= 'a' && name.charAt(i) <= 'z') {                       
                    check = true;
                } else if (name.charAt(i) == '-') {             
                    i++;
                    if(name.charAt(i) >= 'A' && name.charAt(i) <= 'Z') {
                        check = true;                                               
                    } else {
                        check = false;
                    }
                } else if (name.charAt(i) == ' ') {
                    i++;
                    if(name.charAt(i) >= 'A' && name.charAt(i) <= 'Z') {
                        check = true;
                    } else { check = false;
                    }} else { 
                        check = false;
                        break;
                    }
            } 
        } return check;




    }//End checkName()

Can someone help please?

Upvotes: 0

Views: 78

Answers (1)

chrixm
chrixm

Reputation: 1024

This looks like a good place to use a regular expression. What about the following example:

String name = scanner.nextLine();
if (Pattern.compile("^[A-Z][a-z]*(?:(?: |-)[A-Z][a-z]*)?$").matcher(name).find()) {
  // Valid Name
}

This checks the variable name against a regular expression to see if it matches. To explain the regular expression:

^ This means start of string.

[A-Z][a-z]* This means an uppercase letter followed by zero or more lowercase letters.

((?: |-)[A-Z][a-z]*)? This means followed by a space or hyphen with an uppercase followed by an optional lowercase character - this section is optional as the group is followed by a ?.

$ This means end of string.

You can also use String.matches() which is simpler, instead of Pattern.compile().matcher().

Upvotes: 2

Related Questions