Print twice "Wrong date". Conditionals in Java

1

I have to have it print "correct date" when I write a date within the ranges considering months of 28, 30 and 31 days. When I insert by console for example 31/4/1982 (a date that is incorrect because April has 30 days) I print "Wrong date" twice instead of one. Where is the fault?

package Boleto1;

import java.util.*;

public class Ej_16 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner (System.in);
        System.out.println("Introduce un dia");
        int dia=input.nextInt();
        System.out.println("Introduce un mes");
        int mes=input.nextInt();
        System.out.println("Introduce un anio");
        int anio=input.nextInt();

        if (anio==0){
            System.out.println("Fecha incorrecta");
        } else {
            if(mes==2 && (dia>0 && dia<=28)){
                System.out.println("Fecha correcta");
            } else {
                System.out.println("Fecha incorrecta");
            }
            if(mes==4 || mes==6 || mes==9 || mes==11){
                if (dia>0 && dia<=30){
                    System.out.println("Fecha correcta");
                } else {
                    System.out.println("Fecha incorrecta");
                }
            }
            if (mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12){
                if (dia>0 && dia<=31){
                    System.out.println("Fecha correcta");
                } else {
                    System.out.println("Fecha incorrecta");
                }
            }
        }

    }

    }
    
asked by Javi 06.12.2017 в 16:53
source

2 answers

2

Your problem is obviously in how you coded the validation.

Notice that it would have been easy to detect the error, if in each error message you had added a number, such as:

if(mes==2 && (dia>0 && dia<=28)){
    System.out.println("Fecha correcta");
} else {
    System.out.println("Fecha incorrecta 1");
}

Which is exactly where the problem is.

  

Enter a day
  Enter a month
  Enter a year
      Incorrect date 1
      Incorrect date 2

On screen I see that .. Why?

Because that If will always print something, in the right cases, and in the wrong cases. You structured that part of the code not like the rest, if not in another way, and that's why it works differently.

Actually, if structures that if like the others

if(mes==2)
{
    if (dia>0 && dia<=28)){
        System.out.println("Fecha correcta");
    } else {
        System.out.println("Fecha incorrecta");
    }
}

You will stop seeing the problem .. but you will have another, what happens with the leap years?

If this is an exercise of the faculty, I leave you to think about it.

    
answered by 06.12.2017 / 17:04
source
2

You can try to implement your code with LocalDate, which has a method of "of (year, month, dayOfMonth)" that receives as parameters, the day, month and year of integer type, in this way if the date that generate is not valid throw an exception, so you can show the message "Incorrect date" or "Correct date".

Example of LocalDate implementation in your code:

public static void main(String [] args){
  Scanner sc = new Scanner(System.in);

    System.out.println("Introduce un dia");
    int day = sc.nextInt();
    System.out.println("Introduce un mes");
    int month = sc.nextInt();
    System.out.println("Introduce un año");
    int year = sc.nextInt();

    try {
        LocalDate date = LocalDate.of(year, month, day);
        System.out.println("Fecha correcta");
    } catch (Exception e) {
        System.out.println("Fecha incorrecta");
    }
}

This way you avoid generating nested if and the code is not so tangled.

I hope it serves you.

    
answered by 06.12.2017 в 17:35