Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/05/2013, 18:37
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 20 años
Puntos: 1360
Respuesta: Separar cadena String y poner un par de requisitos.

Usa regex para este problema.

Código Java:
Ver original
  1. import java.util.regex.Pattern;
  2. import java.util.regex.Matcher;
  3.  
  4.  
  5. public class Utils {
  6.  
  7.     public static void main(String[] args) {
  8.         String[] tests = new String[]{"ab", "ab1", "ab1-", "2a-b", "321321-asdfasdf-a-46546", ">?<", null};
  9.         for(String test : tests) {
  10.             System.out.println(test + " " + is_valid(test));
  11.         }
  12.     }
  13.  
  14.     public static boolean is_valid(String code) {
  15.  
  16.         if(code == null) return false;
  17.         Pattern p;
  18.         Matcher m;
  19.  
  20.         if(!code.contains("-")) return false;
  21.  
  22.         p = Pattern.compile("\\d");
  23.         m = p.matcher(code);
  24.         if(!m.find()) return false;
  25.  
  26.  
  27.         p = Pattern.compile("\\w(.*)\\w");
  28.         m = p.matcher(code);
  29.         if(!m.find()) return false;
  30.  
  31.  
  32.         return true;
  33.     }
  34. }