Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package tarea4;
  2.  
  3. import java.time.DateTimeException;
  4. import java.time.LocalDate;
  5. import java.time.Month;
  6. import java.util.InputMismatchException;
  7. import java.util.Scanner;
  8. import static util.ES.leeCadena;
  9.  
  10. public class Ejercicio04 {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. // Fecha de inicio del curso
  15. LocalDate fechaInicio = LocalDate.of(2019, Month.SEPTEMBER, 01);
  16. boolean lecturaOk = false;
  17. String cadena;
  18. while (!lecturaOk) {
  19.  
  20. try {
  21.  
  22. cadena = "";
  23.  
  24. do {
  25. System.out.printf("Introduce 4 o más caracteres: ");
  26. cadena = leeCadena();
  27.  
  28. if (cadena.length() < 4) {
  29. System.out.printf("Cadena no válida.");
  30. }
  31. } while (cadena.length() < 4);
  32.  
  33. System.out.println("Introduzca el año : ");
  34. Scanner teclado = new Scanner(System.in);
  35. int anio = teclado.nextInt();
  36.  
  37. // Lectura del año (debe ser 2019 o bien 2020)
  38. while (anio < 2019 || anio > 2020) {
  39. System.out.println("El año debe ser 2019 o 2020.");
  40. anio = teclado.nextInt();
  41. }
  42.  
  43. // Leer el mes (Lógicamente entre 1 y 12)
  44. System.out.println("Introduzca el mes (entre 1 y 12).");
  45. int mes = teclado.nextInt();
  46. while (mes < 1 || mes > 12) {
  47. System.out.println("El mes debe estar entre 1 y 12");
  48. mes = teclado.nextInt();
  49. }
  50.  
  51. // Leer el día
  52. System.out.println("Introduzca el día (entre 1 y 31).");
  53. int dia = teclado.nextInt();
  54.  
  55. // Fecha leída (Saltará una excepción DateTimeException si por
  56. // ejemplo se intenta crear una fecha con 31 de febrero)
  57. LocalDate fechaLeida = LocalDate.of(anio, mes, dia);
  58.  
  59. // Si llegamos aquí es que la lectura ha ido bien y no ha saltado ninguna excepción
  60. lecturaOk = true;
  61.  
  62. for (int contador = 0; contador < fechaInicio.length; contador++) {
  63. if (fechaLeida.isBefore(fechaInicio)) {
  64. fechaLeida[0] = fechaLeida; //esto cambia la primera posicion
  65. }
  66. if (fechaLeida.isAfter(fechaInicio[fechaInicio.lenght])) {
  67. fechaInicio[fechaLeida.length] - 1 = fechaInicio;
  68. }
  69. }
  70. } catch (DateTimeException ex0) {
  71. System.err.println("Error: fecha no válida.\n");
  72. } catch (NumberFormatException ex1) {
  73. System.err.println("Error: no es un número entero válido. ");
  74. } catch (InputMismatchException ex2) {
  75. System.err.println("Error de lectura: formato no válido.");
  76. }
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement