Advertisement
robeeeert

Repaso

May 4th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.34 KB | None | 0 0
  1. /**import java.util.Scanner;
  2.  
  3. public class ValidarDatos {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.  
  7.         // Pedir el nombre
  8.         System.out.print("Ingrese su nombre: ");
  9.         String nombre = input.nextLine();
  10.  
  11.         // Pedir la clave
  12.         System.out.print("Ingrese su clave: ");
  13.         String clave = input.nextLine();
  14.  
  15.         // Validar que la clave solo contenga números
  16.         while (!esNumerica(clave)) {
  17.             System.out.print("La clave solo puede contener números. Por favor, inténtelo de nuevo: ");
  18.             clave = input.nextLine();
  19.         }
  20.  
  21.         // Pedir la fecha
  22.         System.out.print("Ingrese la fecha en formato dd/mm/yyyy: ");
  23.         String fecha = input.nextLine();
  24.  
  25.         // Validar la fecha
  26.         if (esFechaValida(fecha)) {
  27.             System.out.println("¡Bienvenido, " + nombre + "! Su fecha de registro es " + fecha + ".");
  28.         } else {
  29.             System.out.println("La fecha ingresada no es válida. Por favor, inténtelo de nuevo.");
  30.         }
  31.  
  32.         input.close();
  33.     }
  34.  
  35.     public static boolean esNumerica(String cadena) {
  36.         boolean esNumerica = true;
  37.  
  38.         for (int i = 0; i < cadena.length(); i++) {
  39.             char caracter = cadena.charAt(i);
  40.  
  41.             if (caracter < '0' || caracter > '9') {
  42.                 esNumerica = false;
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         return esNumerica;
  48.     }
  49.  
  50.     public static boolean esFechaValida(String fecha) {
  51.         boolean esValida = true;
  52.  
  53.         String[] partesFecha = fecha.split("/");
  54.         int dia = Integer.parseInt(partesFecha[0]);
  55.         int mes = Integer.parseInt(partesFecha[1]);
  56.         int anio = Integer.parseInt(partesFecha[2]);
  57.  
  58.         if (mes < 1 || mes > 12) {
  59.             esValida = false;
  60.         } else {
  61.             switch (mes) {
  62.                 case 2:
  63.                     if (esBisiesto(anio)) {
  64.                         if (dia < 1 || dia > 29) {
  65.                             esValida = false;
  66.                         }
  67.                     } else {
  68.                         if (dia < 1 || dia > 28) {
  69.                             esValida = false;
  70.                         }
  71.                     }
  72.                     break;
  73.                 case 4:
  74.                 case 6:
  75.                 case 9:
  76.                 case 11:
  77.                     if (dia < 1 || dia > 30) {
  78.                         esValida = false;
  79.                     }
  80.                     break;
  81.                 default:
  82.                     if (dia < 1 || dia > 31) {
  83.                         esValida = false;
  84.                     }
  85.                     break;
  86.             }
  87.         }
  88.  
  89.         return esValida;
  90.     }
  91.  
  92.     public static boolean esBisiesto(int anio) {
  93.         return (anio % 4 == 0 && anio % 100 != 0) || (anio % 400 == 0);
  94.     }
  95. }*/
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /**
  119. import java.util.Scanner;
  120.  
  121. public class ValidarDatos {
  122.     public static void main(String[] args) {
  123.         Scanner sc = new Scanner(System.in);
  124.         String nombre, clave, fecha;
  125.         boolean esNumerica = false, esFechaValida = false;
  126.  
  127.         System.out.println("Ingrese su nombre: ");
  128.         nombre = sc.nextLine();
  129.  
  130.         do {
  131.             System.out.println("Ingrese su clave: ");
  132.             clave = sc.nextLine();
  133.             esNumerica = validarNumeros(clave);
  134.             if (!esNumerica) {
  135.                 System.out.println("La clave debe ser numérica.");
  136.             }
  137.         } while (!esNumerica);
  138.  
  139.         do {
  140.             System.out.println("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
  141.             fecha = sc.nextLine();
  142.             esFechaValida = validarFecha(fecha);
  143.             if (!esFechaValida) {
  144.                 System.out.println("Fecha inválida. Intente de nuevo.");
  145.             }
  146.         } while (!esFechaValida);
  147.  
  148.         System.out.println("Nombre: " + nombre);
  149.         System.out.println("Clave: " + clave);
  150.         System.out.println("Fecha de nacimiento: " + fecha);
  151.     }
  152.  
  153.     public static boolean validarNumeros(String cadena) {
  154.         for (int i = 0; i < cadena.length(); i++) {
  155.             char caracter = cadena.charAt(i);
  156.             switch (caracter) {
  157.                 case '0':
  158.                 case '1':
  159.                 case '2':
  160.                 case '3':
  161.                 case '4':
  162.                 case '5':
  163.                 case '6':
  164.                 case '7':
  165.                 case '8':
  166.                 case '9':
  167.                     break;
  168.                 default:
  169.                     return false;
  170.             }
  171.         }
  172.         return true;
  173.     }
  174.  
  175.     public static boolean validarFecha(String fecha) {
  176.         if (fecha.length() != 10) {
  177.             return false;
  178.         }
  179.  
  180.         String diaStr = fecha.substring(0, 2);
  181.         String mesStr = fecha.substring(3, 5);
  182.         String anioStr = fecha.substring(6, 10);
  183.  
  184.         int dia = Integer.parseInt(diaStr);
  185.         int mes = Integer.parseInt(mesStr);
  186.         int anio = Integer.parseInt(anioStr);
  187.  
  188.         if (mes < 1 || mes > 12) {
  189.             return false;
  190.         }
  191.  
  192.         if (dia < 1 || dia > 31) {
  193.             return false;
  194.         }
  195.  
  196.         if (mes == 4 || mes == 6 || mes == 9 || mes == 11) {
  197.             if (dia > 30) {
  198.                 return false;
  199.             }
  200.         } else if (mes == 2) {
  201.             if ((anio % 4 == 0 && anio % 100 != 0) || anio % 400 == 0) {
  202.                 if (dia > 29) {
  203.                     return false;
  204.                 }
  205.             } else {
  206.                 if (dia > 28) {
  207.                     return false;
  208.                 }
  209.             }
  210.         }
  211.  
  212.         return true;
  213.     }
  214. }*/
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225. import java.util.Scanner;
  226.  
  227. public class ValidarDatos {
  228.    
  229.     public static void main(String[] args) {
  230.         Scanner sc = new Scanner(System.in);
  231.         String nombre, clave, fecha;
  232.         boolean claveNumerica, fechaValida;
  233.        
  234.         System.out.print("Ingrese su nombre: ");
  235.         nombre = sc.nextLine();
  236.        
  237.         System.out.print("Ingrese su clave: ");
  238.         clave = sc.nextLine();
  239.         claveNumerica = validarClaveNumerica(clave);
  240.         while (!claveNumerica) {
  241.             System.out.println("La clave debe ser numérica.");
  242.             System.out.print("Ingrese su clave: ");
  243.             clave = sc.nextLine();
  244.             claveNumerica = validarClaveNumerica(clave);
  245.         }
  246.        
  247.         System.out.print("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
  248.         fecha = sc.nextLine();
  249.         fechaValida = validarFecha(fecha);
  250.         while (!fechaValida) {
  251.             System.out.println("La fecha ingresada no es válida.");
  252.             System.out.print("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
  253.             fecha = sc.nextLine();
  254.             fechaValida = validarFecha(fecha);
  255.         }
  256.        
  257.         System.out.println("Datos ingresados correctamente.");
  258.        
  259.         sc.close();
  260.     }
  261.    
  262.     public static boolean validarClaveNumerica(String clave) {
  263.         boolean esNumerica = true;
  264.         for (int i = 0; i < clave.length(); i++) {
  265.             char caracter = clave.charAt(i);
  266.             if (caracter < '0' || caracter > '9') {
  267.                 esNumerica = false;
  268.                 break;
  269.             }
  270.         }
  271.         return esNumerica;
  272.     }
  273.    
  274.     public static boolean validarFecha(String fecha) {
  275.         boolean esFechaValida = true;
  276.         int dia, mes, anio;
  277.         String[] partesFecha = fecha.split("/");
  278.        
  279.         // Validar que haya tres partes
  280.         if (partesFecha.length != 3) {
  281.             esFechaValida = false;
  282.         } else {
  283.             try {
  284.                 dia = Integer.parseInt(partesFecha[0]);
  285.                 mes = Integer.parseInt(partesFecha[1]);
  286.                 anio = Integer.parseInt(partesFecha[2]);
  287.  
  288.                 // Validar el día
  289.                 if (dia < 1 || dia > 31) {
  290.                     esFechaValida = false;
  291.                 } else {
  292.                     switch (mes) {
  293.                         case 2:
  294.                             if (anio % 4 == 0 && (anio % 100 != 0 || anio % 400 == 0)) {
  295.                                 if (dia > 29) {
  296.                                     esFechaValida = false;
  297.                                 }
  298.                             } else {
  299.                                 if (dia > 28) {
  300.                                     esFechaValida = false;
  301.                                 }
  302.                             }
  303.                             break;
  304.                         case 4:
  305.                         case 6:
  306.                         case 9:
  307.                         case 11:
  308.                             if (dia > 30) {
  309.                                 esFechaValida = false;
  310.                             }
  311.                             break;
  312.                         default:
  313.                             if (dia > 31) {
  314.                                 esFechaValida = false;
  315.                             }
  316.                     }
  317.                 }
  318.  
  319.                 // Validar el mes
  320.                 if (mes < 1 || mes > 12) {
  321.                     esFechaValida = false;
  322.                 }
  323.  
  324.             } catch (NumberFormatException e) {
  325.                 esFechaValida = false;
  326.             }
  327.         }
  328.         return esFechaValida;
  329.     }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement