Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 14, 2021 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/a02071b69dc8567d75173cddb4d480fe to your computer and use it in GitHub Desktop.
Save parzibyte/a02071b69dc8567d75173cddb4d480fe to your computer and use it in GitHub Desktop.
public class Main {
public static boolean contieneSoloNumeros(String cadena) {
// Si la cadena está vacía, debemos devolver false
if (cadena.length() == 0) {
return false;
}
for (int x = 0; x < cadena.length(); x++) {
char c = cadena.charAt(x);
// Si no está entre 0 y 9
if (!(c >= '0' && c <= '9')) {
return false;
}
}
return true;
}
public static boolean contieneSoloNumerosRegex(String cadena) {
return cadena.matches("[0-9]+");
}
public static void main(String[] args) {
System.out.println(contieneSoloNumeros("123"));
System.out.println(contieneSoloNumeros("ae4"));
System.out.println(contieneSoloNumeros("12e"));
System.out.println(contieneSoloNumeros("ddd"));
System.out.println(contieneSoloNumeros(""));
System.out.println("---");
System.out.println(contieneSoloNumerosRegex("123"));
System.out.println(contieneSoloNumerosRegex("ae4"));
System.out.println(contieneSoloNumerosRegex("12e"));
System.out.println(contieneSoloNumerosRegex("ddd"));
System.out.println(contieneSoloNumerosRegex(""));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment