java - How do I go through the String to make sure it only contains numbers in certain places? -


i writing take user's input phone number , tell user if input valid or not based on 5 parameters:

  1. input 13 characters in length
  2. char @ index 0 '('
  3. char @ index 4 ')'
  4. char @ index 8 '-'
  5. all other characters must 1 of digits: ’0’ through ’9’ inclusive.

so far have down except 5th parameter. code goes followed

   if (number.contains("[0-9]+"))     {         ints = true;          if (number.contains("[a-za-z]*\\d+."))         {             ints = false;         }     }     else      {         ints = false;     } 

(side note: number string user's input, , ints boolean declared earlier in code).

you can use following. if string correct print valid, otherwise print invalid.

public void compare(){             string inputstring="(123)848-3452";     if(inputstring.matches("^\\([0-9]{3}\\)[0-9]{3}-[0-9]{4}")){         system.out.println("valid");     }else{         system.out.println("invalid");     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -