I am trying to make a program that will allow the user to input two sets of name and birthday and will compare whether the inputs are identical.
I have a separate class named MyDate which contains the month, day and year, thus I created an object which I named "dob" in my person class, which contains the String name and MyDate dob . Upon creating a test program, I'm having difficulty on how to get the birthday input and set it as "MM-dd-yyyy" since I cannot use the java.util.Date method.
This is what I have so far:
System.out.println("Enter birthdate: ");
String bd=console.next();
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
MyDate dob = sdf.parse(bd);
p1.setDob(dob);
Any idea on how to fix it? any help would be appreciated
class MyDate for this program:
public class MyDate {
private int day;
private int month;
private int year;
public MyDate() {
}
public MyDate(int month, int day, int year) {
setDay(month);
setMonth(day);
setYear(year);
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int hashCode() {
return this.getMonth() ^ this.getDay() ^ this.getYear();
}
public String toString() {
return this.month "-" this.day "-" this.year " ";
}
}
The program's output should look similar to this:
enter name: jay
enter birthday: 02-07-1999
enter name: jay
enter birthday: 02-07-1999
IDENTICAL
name: jay
birthday: 02-07-1999
name: jay
birthday: 02-07-1999
CodePudding user response:
First off, you cannot do this MyDate dob = sdf.parse(bd);
According to the Java doc, https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html, SimpleDateformat parse method returns Date object while your dob has MyDate. you cant mix both type.
What you want to do is to take the input from the user and pass it to your MyDate constructor. I have a follow up question, since you are not allowed to use Java.util.Date, are you allowed to use the SimpleDateFormat given that the parse return Date?
Assuming that your program is a simple program and advance validation is not required, you can use split by - on the input which will give you an array of 3 items and you can pass them to MyDate based on the index.
In order to do identical check, you will need to override the Equals method in your Person class
CodePudding user response:
This is one possible solution
System.out.print("Enter birthdate in the following format MM-dd-YYYY: ");
String bd=console.next();
That might not be as good as the following solution:
Scanner console = new Scanner(System.in);
System.out.println("Enter the number your birthdate month (i.e. 1 for January) : ");
int mm = console.nextInt();
System.out.println("Enter your birth day : ");
int dd = console.nextInt();
System.out.println("Enter the four digits of your birthdate year: ");
int yyyy = console.nextInt();
console.close();
StringBuilder buffer = new StringBuilder();
buffer.append(String.format("d", mm));
buffer.append("-");
buffer.append(String.format("d", dd));
buffer.append("-");
buffer.append(String.format("d", yyyy));
String date = buffer.toString();
System.out.println(date);
This is more verbose, but I think it is a better solution than the first one. The result:
Enter the number your birthdate month (i.e. 1 for January) :
7
Enter your birth day :
10
Enter the four digits of your birthdate year:
1968
07-10-1968
The above solution can be transformed into what the OP wants very easily.
Scanner console = new Scanner(System.in);
System.out.println("Enter the number your birthdate month (i.e. 1 for January) : ");
int mm = console.nextInt();
System.out.println("Enter your birth day : ");
int dd = console.nextInt();
System.out.println("Enter the four digits of your birthdate year: ");
int yyyy = console.nextInt();
console.close();
MyDate dob = new MyDate(mm, dd, yyyy);
The MyDate
class should be modified with the rest of my original code as follows:
public class MyDate {
// rest of code omitted
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(String.format("d", mm));
buffer.append("-");
buffer.append(String.format("d", dd));
buffer.append("-");
buffer.append(String.format("d", yyyy));
return buffer.toString();
}
}