I\'m writing a code that takes two dates and prints which one is earlier. The pr
ID: 647072 • Letter: I
Question
I'm writing a code that takes two dates and prints which one is earlier. The problem is the user is supposed to input the date as mm/day/year and i can't figure out how to take the input in java and change it so it can accept it in that format. Insted I've set it up so the user enters the month, then the day, then the year. Can anyone help me figure out how to take the input in the proper format? Here's what I have:
public static int processPerson(Scanner console, int today, int num, int year, String name) {
int month =0;
int day =0;
//ask the user the date of birth in the format m/d as a string
System.out.print("What month and day was " + name + " born, month/day?");
//THIS IS THE PART I'M TALKING ABOUT
month = console.nextInt();
day = console.nextInt();
//seperate the tokens to day and month
//convert each token to its integer value and store them in month and day variables
int birthday = dayOfYear(month, day);
String m = monthName(month);
//output the inforamtion about the person's birthday
int days = daysTillBday(birthday, today);
if (days == 0) {
System.out.println("Oh my god it is your birthday, Happy birthday!!!!!!!!");
} else {
System.out.print(m + "," + day + "," + year + " falls on day #" + birthday + " of 365. ");
System.out.print(name + " birthday is in " + days + " day(s).");
double percentage = 100.0 * days / 366;
System.out.printf(" That is %.1f percent of a year away. ", percentage);
}
System.out.println();
return days;
}
Explanation / Answer
public static int processPerson(Scanner console, int today, int num, int year, String name) {
int month =0;
int day =0;
//ask the user the date of birth in the format m/d as a string
System.out.print("What month and day was " + name + " born, month/day?");
String myDate = in.next(); // enter input like mm/dd/yy
String[] array = myDate.split("/");
month = Integer.parseInt(array[0]);
day = Integer.parseInt(array[1]);
//seperate the tokens to day and month
//convert each token to its integer value and store them in month and day variables
int birthday = dayOfYear(month, day);
String m = monthName(month);
//output the inforamtion about the person's birthday
int days = daysTillBday(birthday, today);
if (days == 0) {
System.out.println("Oh my god it is your birthday, Happy birthday!!!!!!!!");
} else {
System.out.print(m + "," + day + "," + year + " falls on day #" + birthday + " of 365. ");
System.out.print(name + " birthday is in " + days + " day(s).");
double percentage = 100.0 * days / 366;
System.out.printf(" That is %.1f percent of a year away. ", percentage);
}
System.out.println();
return days;
}