Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**import java.util.Scanner;
- public class ValidarDatos {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- // Pedir el nombre
- System.out.print("Ingrese su nombre: ");
- String nombre = input.nextLine();
- // Pedir la clave
- System.out.print("Ingrese su clave: ");
- String clave = input.nextLine();
- // Validar que la clave solo contenga números
- while (!esNumerica(clave)) {
- System.out.print("La clave solo puede contener números. Por favor, inténtelo de nuevo: ");
- clave = input.nextLine();
- }
- // Pedir la fecha
- System.out.print("Ingrese la fecha en formato dd/mm/yyyy: ");
- String fecha = input.nextLine();
- // Validar la fecha
- if (esFechaValida(fecha)) {
- System.out.println("¡Bienvenido, " + nombre + "! Su fecha de registro es " + fecha + ".");
- } else {
- System.out.println("La fecha ingresada no es válida. Por favor, inténtelo de nuevo.");
- }
- input.close();
- }
- public static boolean esNumerica(String cadena) {
- boolean esNumerica = true;
- for (int i = 0; i < cadena.length(); i++) {
- char caracter = cadena.charAt(i);
- if (caracter < '0' || caracter > '9') {
- esNumerica = false;
- break;
- }
- }
- return esNumerica;
- }
- public static boolean esFechaValida(String fecha) {
- boolean esValida = true;
- String[] partesFecha = fecha.split("/");
- int dia = Integer.parseInt(partesFecha[0]);
- int mes = Integer.parseInt(partesFecha[1]);
- int anio = Integer.parseInt(partesFecha[2]);
- if (mes < 1 || mes > 12) {
- esValida = false;
- } else {
- switch (mes) {
- case 2:
- if (esBisiesto(anio)) {
- if (dia < 1 || dia > 29) {
- esValida = false;
- }
- } else {
- if (dia < 1 || dia > 28) {
- esValida = false;
- }
- }
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- if (dia < 1 || dia > 30) {
- esValida = false;
- }
- break;
- default:
- if (dia < 1 || dia > 31) {
- esValida = false;
- }
- break;
- }
- }
- return esValida;
- }
- public static boolean esBisiesto(int anio) {
- return (anio % 4 == 0 && anio % 100 != 0) || (anio % 400 == 0);
- }
- }*/
- /**
- import java.util.Scanner;
- public class ValidarDatos {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String nombre, clave, fecha;
- boolean esNumerica = false, esFechaValida = false;
- System.out.println("Ingrese su nombre: ");
- nombre = sc.nextLine();
- do {
- System.out.println("Ingrese su clave: ");
- clave = sc.nextLine();
- esNumerica = validarNumeros(clave);
- if (!esNumerica) {
- System.out.println("La clave debe ser numérica.");
- }
- } while (!esNumerica);
- do {
- System.out.println("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
- fecha = sc.nextLine();
- esFechaValida = validarFecha(fecha);
- if (!esFechaValida) {
- System.out.println("Fecha inválida. Intente de nuevo.");
- }
- } while (!esFechaValida);
- System.out.println("Nombre: " + nombre);
- System.out.println("Clave: " + clave);
- System.out.println("Fecha de nacimiento: " + fecha);
- }
- public static boolean validarNumeros(String cadena) {
- for (int i = 0; i < cadena.length(); i++) {
- char caracter = cadena.charAt(i);
- switch (caracter) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- break;
- default:
- return false;
- }
- }
- return true;
- }
- public static boolean validarFecha(String fecha) {
- if (fecha.length() != 10) {
- return false;
- }
- String diaStr = fecha.substring(0, 2);
- String mesStr = fecha.substring(3, 5);
- String anioStr = fecha.substring(6, 10);
- int dia = Integer.parseInt(diaStr);
- int mes = Integer.parseInt(mesStr);
- int anio = Integer.parseInt(anioStr);
- if (mes < 1 || mes > 12) {
- return false;
- }
- if (dia < 1 || dia > 31) {
- return false;
- }
- if (mes == 4 || mes == 6 || mes == 9 || mes == 11) {
- if (dia > 30) {
- return false;
- }
- } else if (mes == 2) {
- if ((anio % 4 == 0 && anio % 100 != 0) || anio % 400 == 0) {
- if (dia > 29) {
- return false;
- }
- } else {
- if (dia > 28) {
- return false;
- }
- }
- }
- return true;
- }
- }*/
- import java.util.Scanner;
- public class ValidarDatos {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String nombre, clave, fecha;
- boolean claveNumerica, fechaValida;
- System.out.print("Ingrese su nombre: ");
- nombre = sc.nextLine();
- System.out.print("Ingrese su clave: ");
- clave = sc.nextLine();
- claveNumerica = validarClaveNumerica(clave);
- while (!claveNumerica) {
- System.out.println("La clave debe ser numérica.");
- System.out.print("Ingrese su clave: ");
- clave = sc.nextLine();
- claveNumerica = validarClaveNumerica(clave);
- }
- System.out.print("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
- fecha = sc.nextLine();
- fechaValida = validarFecha(fecha);
- while (!fechaValida) {
- System.out.println("La fecha ingresada no es válida.");
- System.out.print("Ingrese su fecha de nacimiento en formato dd/mm/yyyy: ");
- fecha = sc.nextLine();
- fechaValida = validarFecha(fecha);
- }
- System.out.println("Datos ingresados correctamente.");
- sc.close();
- }
- public static boolean validarClaveNumerica(String clave) {
- boolean esNumerica = true;
- for (int i = 0; i < clave.length(); i++) {
- char caracter = clave.charAt(i);
- if (caracter < '0' || caracter > '9') {
- esNumerica = false;
- break;
- }
- }
- return esNumerica;
- }
- public static boolean validarFecha(String fecha) {
- boolean esFechaValida = true;
- int dia, mes, anio;
- String[] partesFecha = fecha.split("/");
- // Validar que haya tres partes
- if (partesFecha.length != 3) {
- esFechaValida = false;
- } else {
- try {
- dia = Integer.parseInt(partesFecha[0]);
- mes = Integer.parseInt(partesFecha[1]);
- anio = Integer.parseInt(partesFecha[2]);
- // Validar el día
- if (dia < 1 || dia > 31) {
- esFechaValida = false;
- } else {
- switch (mes) {
- case 2:
- if (anio % 4 == 0 && (anio % 100 != 0 || anio % 400 == 0)) {
- if (dia > 29) {
- esFechaValida = false;
- }
- } else {
- if (dia > 28) {
- esFechaValida = false;
- }
- }
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- if (dia > 30) {
- esFechaValida = false;
- }
- break;
- default:
- if (dia > 31) {
- esFechaValida = false;
- }
- }
- }
- // Validar el mes
- if (mes < 1 || mes > 12) {
- esFechaValida = false;
- }
- } catch (NumberFormatException e) {
- esFechaValida = false;
- }
- }
- return esFechaValida;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement