-
-
Save parzibyte/a02071b69dc8567d75173cddb4d480fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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