Reputation: 1
hi im trying to fix this program,
Write a Java program that displays the calendar of any month of any year between 1900 and 2099.
The program must:
1. Prompt the user for a specific month with a meaningful message.
2.1. If the input value is not a valid date, the user must be informed that date was not acceptable, and then be prompted again for another date. If more than three consecutive erroneous dates are given, the program must be terminated with an appropriate error message.
2.2. After displaying the calendar, the program must ask whether or not the user wants to enter another date (Do you want to continue?)
2.3. The user must answer either yes, no, y or n, in any combination of lower and upper case letters
2.4. If an invalid answer is given, the user must be informed that the answer was not acceptable, and then be prompted again for another answer. If more than three consecutive erroneous answers are given, the program must be terminated with an appropriate error message
2.5. If the answer in step 2.2, is Yes (in any allowed form) the program must continue from step 1. If the answer is No (in any allowed form) the program must terminate with a message informing that the program is terminated normally.
All input must be done using an Scanner object i.e. Scanner kb = new Scanner (System.in);
All output should be done using System.out.print or System.out.println
so far i have gotten up to 2.1 my if statement in the program tells the user that the date was wrong however i am struggling how to then ask the user for another date, and if the user keeps putting in an incorrect date doing this 3 times before terminating the program, and if the user enters the right date, continuing to print the calendar.
this is what i have so far
public static int getMonthNumber(String s) {
if (s.equalsIgnoreCase("jan")) {
return 1;
}
if (s.equalsIgnoreCase("feb")) {
return 2;
}
if (s.equalsIgnoreCase("mar")) {
return 3;
}
if (s.equalsIgnoreCase("apr")) {
return 4;
}
if (s.equalsIgnoreCase("may")) {
return 5;
}
if (s.equalsIgnoreCase("jun")) {
return 6;
}
if (s.equalsIgnoreCase("jul")) {
return 7;
}
if (s.equalsIgnoreCase("aug")) {
return 8;
}
if (s.equalsIgnoreCase("sep")) {
return 9;
}
if (s.equalsIgnoreCase("oct")) {
return 10;
}
if (s.equalsIgnoreCase("nov")) {
return 11;
}
if (s.equalsIgnoreCase("dec")) {
return 12;
} else {
System.out.println("Not valid month!");
}
return 0;
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 28;
} else {
return 29;
}
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
return -1;
}
}
public static boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static String getMonthName(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month.";
}
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days = days + 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(i, year);
}
int startday = (days % 7) + 2;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i <= startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
public static void main(String[] args) {
Scanner scan = null;
Scanner kb = new Scanner(System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
System.out.print(" ");
String month;
int year;
month = kb.next();
year = kb.nextInt();
int yearno = year;
int monthno = getMonthNumber(month);
if (year > 2099)
System.out.println(" Invalid Year!");
if (year < 1900);
System.out.println(" Invalid Year!");
System.out.println();
displayCalendar(monthno, yearno);
System.out.println();
System.out.println();
System.out.println("Do you want to continue? y/n ");
if the year is wrong it prints invalid year, but now m stuck on how to ask again 3 ties before ending the program/ continuing if the user enters the right date on one of the 3 tries. so far with my program, if the user enters a valid date the correct calender is printed
Upvotes: 0
Views: 247
Reputation: 44824
what you need to do is loop
int monthno = 0;
while (monthno == 0) {
System.out.print("Give the first three letters of a month");
month = kb.next();
monthno = getMonthNumber(month);
}
and do the same for other inputs
BTW your code would be a lot clean if you kept tour months
in a ArrayList
and then simply did a get
to get the value. It would either exist or not.
Upvotes: 0