java - How to make a program ask for a input date multiple times if wrong -


hi im trying fix program,

write java program displays calendar of month of year between 1900 , 2099.

the program must:
1. prompt user specific month meaningful message.

  1. if input value valid date (i.e. 3 characters designating desired month (i.e. jan, feb, ..., in combination of lower , upper case letters, followed space , integer between 1900 , 2099), calendar given date must displayed

2.1. if input value not valid date, user must informed date not acceptable, , prompted again date. if more 3 consecutive erroneous dates given, program must terminated appropriate error message.

2.2. after displaying calendar, program must ask whether or not user wants enter date (do want continue?)

2.3. user must answer either yes, no, y or n, in combination of lower , upper case letters

2.4. if invalid answer given, user must informed answer not acceptable, , prompted again answer. if more 3 consecutive erroneous answers given, program must terminated appropriate error message

2.5. if answer in step 2.2, yes (in allowed form) program must continue step 1. if answer no (in allowed form) program must terminate message informing program terminated normally.

  1. all input must done using scanner object i.e. scanner kb = new scanner (system.in);

  2. all output should done using system.out.print or system.out.println

so far have gotten 2.1 if statement in program tells user date wrong struggling how ask user date, , if user keeps putting in incorrect date doing 3 times before terminating program, , if user enters right date, continuing print calendar.

this have 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;     (int = 1900; < year; i++) {         days = days + 365;         if (isleapyear(i)) {             days = days + 1;          }     }     (int = 1; < 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;     (int = 1; <= startday; = + 1) {         system.out.print("    ");     }     (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 first 3 letters of month , enter 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 want continue? y/n "); 

if year wrong prints invalid year, m stuck on how ask again 3 ties before ending program/ continuing if user enters right date on 1 of 3 tries. far program, if user enters valid date correct calender printed

what need loop

  int monthno = 0;   while (monthno == 0) {      system.out.print("give first 3 letters of month");      month = kb.next();      monthno = getmonthnumber(month);            } 

and same other inputs

btw code lot clean if kept tour months in arraylist , did get value. either exist or not.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -