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:
- input 13 characters in length
- char @ index 0 '('
- char @ index 4 ')'
- char @ index 8 '-'
- 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
Post a Comment