Alexis-Coria

Helper

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