Patch
Patch

Reputation: 233

See if a string contains any characters in it

The problem I'm having is when I check to see if the string contains any characters it only looks at the first character, not the whole string. For instance, I would like to be able to input "123abc" and the characters are recognized so it fails. I also need the string to be 11 characters long and since my program only works with 1 character it cannot go any further.

Here is my code so far:

public static int phoneNumber(int a)
{
   while (invalidinput)
            {
        phoneNumber[a] = myScanner.nextLine();
        
        if (phoneNumber[a].matches("[0-9+]") && phoneNumber[a].length() == 11 )
        {   
            System.out.println("Continue");
            invalidinput = false;
        }
        else
        {
            System.out.print("Please enter a valid phone number: ");
        }
        
    }
    
    return 0;
}

For instance why if I take away the checking to see the phoneNumber.length(), it still only registers 1 character; so, if I enter "12345", it still fails. I can only enter "1" for the program to continue.

If someone could explain how this works to me that would be great.

Upvotes: 7

Views: 10932

Answers (5)

anubhava
anubhava

Reputation: 785856

Your regex and if condition is wrong. Use it like this:

 if ( phoneNumber[a].matches("^[0-9]{11}$") ) {
    System.out.println("Continue");
    invalidinput = false;
 }

This will allow only phoneNumber[a] to be a 11 characters long comprising only digits 0-9.

Upvotes: 7

Are the legal characters in your phone numbers 0..9 and +? If so, then you should use the regular expression [0-9+]*, which matches zero or more legal characters. (If not, you probably meant [0-9]+.) Also, you can use [0-9+]{11} instead of your explicit check for a length of 11.

The reason that your current code fails, is that String#matches() does not check whether the regular expression matches part of the string, but whether it matches all of the string. You can see this in the JavaDoc, which points you to Matcher#matches(), which "Attempts to match the entire region against the pattern."

Upvotes: 0

Lauripops
Lauripops

Reputation: 442

Why not try using a for loop to go through each character?

Like:

public static int phoneNumber(int a)
{
   while (invalidinput)
            {

       int x = 0;
       for(int i = 0; i < phoneNumber[a].length(); i++)
       {
        char c = phoneNumber[a].charAt(i);
        if(c.matches("[0-9+]")){
        x++;
       }


}
  if (x == phoneNumber[a].length){
    System.out.println("Continue");
    invalidinput = false;

        }
  else
        {
            System.out.print("Please enter a valid phone number: ");
        }

}
return 0;
}

Upvotes: 0

forgivenson
forgivenson

Reputation: 4435

You need to put the "+" after the "]" in your regex. So, you would change it to:

phoneNumber[a].matches("[0-9]+")

Upvotes: 0

dr01d3k4
dr01d3k4

Reputation: 91

The + should be outside the set, or you could specifically try to match 11 digits like this: ^[0-9]{11}$ (the ^ and $ anchor the match to the start and end of the string).

Upvotes: 0

Related Questions