Why do I have an error in the code when creating the date object in java?

0
package libro.cap02.fechas;
import  java.util.Scanner;
public class TestFecha {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);

{
    System.out.println("Ingrese: (dia mes y año)");

    int dia = scanner.nextInt();
    int mes = scanner.nextInt();
    int año = scanner.nextInt();

    Fecha f1 = new Fecha(dia, mes, año);


        System.out.print ("Ingrese : (dia mes y año)");

        dia = scanner.nextInt();
        mes = scanner.nextInt();
        año = scanner.nextInt();

    Fceha f2 = new Fecha (dia,mes,año);

    System.out.println("Fecha 1 = " +f1);
    System.out.println("Fecha 2 = " +f2);

    if (f1.equals(f2))
     {
          System.out.println("Son iguales");
     }

     else
     {
         System.out.println("Son distintas...");
     }
}}}
    
asked by Francisco Gonzalez 04.10.2018 в 03:45
source

1 answer

0

I think the error is because it is badly invoked, either by the part that needs a constructor, or that you do not have methods to enter the data properly.

I guess the "Date" class looks like this:

public class Fecha {
    private int dia, mes, anio;

    public void setDia(int dia) {
        this.dia = dia;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }

    public void setAnio(int anio) {
        this.anio = anio;
    }
}

If so, you would have to add the values as follows:

public class main {

    public static void main(String[] args) {
        Fecha f1 = new Fecha();
        f1.setAnio(2018);
        f1.setMes(10);
        f1.setDia(04);

        Fecha f2 = new Fecha();
        f2.setAnio(2018);
        f2.setMes(10);
        f2.setDia(04);

        if (f1.equals(f2)) {
            System.out.println("Son iguales");
        } else {
            System.out.println("Son distintas...");
        }
    }
}

Another way is adding a constructor to the "Date" class:

public class Fecha {
    private int dia, mes, anio;

    public Fecha(int dia, int mes, int anio) {
        this.dia = dia;
        this.mes = mes;
        this.anio = anio;
    }
}

And when invoked, they are entered in the following way:

public class main {

    public static void main(String[] args) {

        Fecha f3 = new Fecha(04, 10, 2018);
        Fecha f4 = new Fecha(04, 10, 2018);

        if (f3.equals(f4)) {
            System.out.println("Son iguales");
        } else {
            System.out.println("Son distintas...");
        }
    }
}

Greetings

    
answered by 04.10.2018 в 23:47