I would like to instantiate an object of another class via scanner, but one of the parameters I need to pass is a LocalDate type. This is why I created a method in my main class in order to
- ask the user for a String input in a given order (for example dd. MMM. yyyy)
- my program then takes that input and saves it as string to display it back to the user later on
But I'm stuck with an error. Below is my birthday class and the relevant parts of my main class.
Birthday.java:
public class Birthday {
String name;
LocalDate date;
int age;
boolean gift;
public Birthday(String name, LocalDate date, int age, boolean gift) {
this.name = name;
this.date = date;
this.age = age;
this.gift = gift;
}
}
Main.java:
public static void addBirthday(){
Scanner scan = new Scanner(System.in);
//below I want to instantiate a Birthday obj
Birthday bday = new Birthday(scan.nextLine(), addDate(), scan.nextInt(), scan.nextBoolean());
bdays.add(bday); //this is referring to an ArrayList I didn't include in this excerpt
scan.close();
}
...
public static String addDate() {
Scanner scan = new Scanner(System.in);
LocalDate ld = LocalDate.of(scan.nextInt(), scan.nextInt(), scan.nextInt());
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd. MMM. yyyy");
System.out.println(ld.format(dtf));
scan.close();
//return addedDate??;
}
the error I keep getting for the instantiation is "incompatible types: java.lang.String cannot be converted to java.time.LocalDate", making the addDate() Method void leads to another error saying the method can't be used.
the commented out return statement is what I thought could be possible: printing out System.out.println(ld.format(dtf));
and storing it in a String called addedDate? But seems like a very wrong thought process on my part, so in the end I'm wondering if my approach in the addDate() method where I ask for year/month/day separately is what's making it wrong?