LoianePJ
Não sei se é exatamente isso que você quer, mas tá aí:
public class Valida
{
public static boolean validaTelefone (String numeroTelefone)
{
//se não tiver 9 dígitos, retorna falso
if (numeroTelefone.length() != 9)
return false;
//verifica se todos os caracteres são números
// caso algum não seja, telefone inválido
for (int i =0; i< numeroTelefone.length(); i++)
if (! Character.isDigit(numeroTelefone.charAt(i)))
return false;
//verifica se começa com 2 ou 9
if (numeroTelefone.charAt(0) == '2')
return true;
else if (numeroTelefone.charAt(0) == '9')
{
//verifica se segundo dígito é 1, 3 ou 6
char segundoDigito = numeroTelefone.charAt(1);
switch (segundoDigito)
{
case 1:
case 3:
case 6: return true;
}
}
return false;
}
}
Isso levando em consideração que os outros dígitos podem ser qualquer número.
Espero ter ajudado!
[]'s
jtmartinsPJ
ajudou e MUITO!!!
a minha ideia foi :
//para os telefones começados por 9
public boolean IsValidTelemovel(String tm){
if(Pattern.matches("2.[0-9]+", tm)&&tm.length()==9)
return true;
return false;
}
// para os telefones começados por 2
public boolean IsValidFax(String fx){
if(Pattern.matches("2.[0-9]+", fx)&&fx.length()==9)
return true;
return true;
}
mas a tua ideia foi bem melhor, obg 