Alexis-Coria

Helper

Nov 8th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import java.util.*;
  2. public class Helper {
  3. //validar numeros positi
  4. public static int validarPositivo(Scanner opcion , String mensaje){
  5. int numero;
  6. String linea;
  7. while (true) {
  8. try {
  9. System.out.println(mensaje);
  10. linea = opcion.nextLine();
  11. numero = Integer.parseInt(linea);
  12. while(!(numero>=0)){
  13. System.out.println("''ERROR:Ingrese un numero positivo!''");
  14. System.out.println(mensaje);
  15. linea = opcion.nextLine();
  16. numero = Integer.parseInt(linea);
  17. }
  18. break;
  19. } catch (Exception e) {
  20. System.out.println("''ERROR..! Ingrese un numero''");
  21. }
  22. }
  23. return numero;
  24. }
  25. //validar opcion random o manual
  26. public static int elegirOpcion(Scanner opcion , String mensaje){
  27. int numero;
  28. String linea;
  29. while (true) {
  30. try {
  31. System.out.println(mensaje);
  32. linea = opcion.nextLine();
  33. numero = Integer.parseInt(linea);
  34. while(!(numero<=4 && numero>=1)){
  35. System.out.println("Opcion Incorrecta");
  36. System.out.println(mensaje);
  37. linea = opcion.nextLine();
  38. numero = Integer.parseInt(linea);
  39. }
  40. break;
  41. } catch (Exception e) {
  42. System.out.println("''ERROR..! Ingrese un numero''");
  43. }
  44. }
  45. return numero;
  46. }
  47.  
  48. //validar que se ingrese solamente letras primera parte
  49. public static String cargarLetra(Scanner entrada,String mensaje){
  50. System.out.println(mensaje);
  51. String linea=entrada.nextLine();
  52. while(validarLetras(linea)){
  53. System.out.println("''ERROR'' No Ingresar Numeros");
  54. System.out.println(mensaje);
  55. linea=entrada.nextLine();
  56. }
  57. return linea;
  58. }
  59. //validar que se ingrese solamente letras segunda parte
  60. public static boolean validarLetras(String linea) {
  61. for (int x = 0; x < linea.length(); x++) {
  62. char c = linea.charAt(x);
  63. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ' ')) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. //Genera caracter random
  71. public static String caracterRandom() {
  72. Random random = new Random();
  73. char caracterRandom = (char)(random.nextInt(26) + 'a');
  74. System.out.println("Caracter generado: "+ caracterRandom);
  75. String caracter = String.valueOf(caracterRandom);
  76. return caracter;
  77. }
  78.  
  79.  
  80. //Genera random con parametro a peticion
  81. public static int generarRandom(String mensaje,int x) {
  82. System.out.println(mensaje);
  83. int numRandom = (int) (Math.random() * x);
  84. System.out.println("Valor generado: "+numRandom );
  85. return numRandom;
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment