0

I'm currently making a calendar for a college course. I had previously made a calendar that was fully functional at that point. In this version, I was to touch upon the calendar and make a menu, which I did. So basically I copy-pasted my older calendar code into my code because it was the same end result he wanted.

Here's the issue: A method called monthFromDate which cuts a string down to size and converts it to an integer is getting called prematurely when it should be waiting for user input. I suspect the same might happen to dayFromDate.

Here is the code in entirety:

import java.util.*;
class SuperGlobal {
   public static Scanner s = new Scanner(System.in);
   public static Calendar c = Calendar.getInstance();
   public static boolean InProgress = false;
}
public class MBAssignment2 extends SuperGlobal{
   public static void main(String args[]) {
      boolean Status = true, Error = false;
      while (Status == true) {
         System.out.println("Please choose a command");
         System.out.printf("\t\"e\" to enter a date and display the corresponding calendar.%n");
         System.out.printf("\t\"t\" to get todays date and display todays calandar.%n");
         System.out.printf("\t\"n\" to display the next month.%n");
         System.out.printf("\t\"p\" to display the previous month.%n");
         System.out.printf("\t\"q\" to quit program.%n");
         var Command = s.next();
         do {
            if (Command instanceof String) {
            Error = false;
               if (Command.equals("e")) {
                  UserDate();
                  InProgress = true;
               }
               else if (Command.equals("t")) {
                  System.out.println("Coming soon...");
               }
               else if (Command.equals("n")) {
                  if (InProgress == true) {
                     System.out.println("Coming soon...");
                  }
                  else {
                     System.out.println("Please have a calendar displayed.");
                     Error = true;
                  }
               }
               else if (Command.equals("p")) {
                  if (InProgress == true) {
                     System.out.println("Coming soon...");
                  }
                  else {
                     System.out.println("Please have a calendar displayed.");
                     Error = true;
                  }
               }
               else if (Command.equals("q")) {
                  System.out.println("Exiting...");
                  Status = false;
                  Error = false;
               }
               else {
                  System.out.println("Please enter a valid command.");
                  Error = true;
               }
            }
            if (Error == true) {
               System.out.println("Please choose a command");
               System.out.printf("\t\"e\" to enter a date and display the corresponding calendar.%n");
               System.out.printf("\t\"t\" to get todays date and display todays calandar.%n");
               System.out.printf("\t\"n\" to display the next month.%n");
               System.out.printf("\t\"p\" to display the previous month.%n");
               System.out.printf("\t\"q\" to quit program.%n");
               Command = s.next();
            }
         } while(Error == true);
      }
   }
   public static void UserDate (){
      int cm = c.get(Calendar.MONTH);                 // Get and store current month value
      int cd = c.get(Calendar.DATE);                  // Get and store current day value

      System.out.println("Desired date(mm/dd)-->");   // User input (As a String)
      String inp = s.nextLine();
      if (inp instanceof String) {
         int month = monthFromDate(inp);              // Get month value via sending user input to seperate method
         int day = dayFromDate(inp);                  // Get day value via sending user input to seperate method
         if (month > 12 || day > 31) {                // Check for valid user input
            System.out.print("|===INVALID DATE===|");
         }
         else {
            drawMonth(month);                         // Call method to create calendar with user input
            displayDate(month, day);                  // Write users month and date via seperate method under calender
            System.out.println("This month: ");
            drawMonth(cm);                            // Draw current months calendar
            displayDate(cm, cd);                      // Write current month and date under second calendar
         }
      }
   }
   public static void drawMonth(int month) {          // Method to draw calendars
      System.out.println("_____/\\/\\/\\/\\^*^*^*^*^*^*``````~~-#-~~``````*^*^*^*^*^*^/\\/\\/\\/\\_____"); // ASCII Art
      System.out.print("                                  ");
      System.out.print(month);                        // Month value
      System.out.printf("                                   %n");
      for(int i = 0; i < 70; i++) {                   // Row barrier
         System.out.print("=");
      }
      System.out.printf("%n");                        // Line break
      for(int i = 0; i < 5; i++) {                    // Call row method 5 times (send which row it is every iteration)
         drawRow(i);
      }
   }

   public static int monthFromDate(String date) {
      int cut = date.indexOf("/");                    // Find slash in user input string
      String m1 = date.substring(0, cut);             // Isolate month portion of user input
      int m2 = Integer.parseInt(m1);                  // Convert string to Integer

      return(m2);                                     // Return month value
   }

   public static int dayFromDate(String date) {
      int cut = date.indexOf("/") + 1;                // Find slash and offset one spot to the right
      String d1 = date.substring(cut);                // Isolate day value
      int d2 = Integer.parseInt(d1);                  // Convert string to Integer

      return(d2);                                     // Return day value
   }
   public static void drawRow(int row) {
      int rn = row * 7 + 1;                           // Calculation of which row (i.e. loop number 3 (i = 2) so 2 * 7 = 14 + 1 = 15)
      String check = " ";                             // Buffer String to make the calendar look nice
      for(int i = 0; i < 7; i++) {                    // Loop to draw row numbers
         if (rn > 9) {                                // Check to see if row number is above 9 (double digits)
            check = "";                               // If so, remove a space to keep total length the same
         }
         else {
            check = " ";                              // Reset for Buffer
         }
         System.out.print("| " + rn + "   " + check + "   ");     // Print first row of a singular day
         rn++;                                        // Row number + 1
         if(rn > 31){                                 // Check to see if row number went above 31
            rn = 1;                                   // If so, reset row number to one
         }
      }
      System.out.println("|");                        // End of row vertical line cap
      for(int i = 0; i < 4; i++) {                    // Nested for loops to finish a row
         for(int ii = 0; ii < 7; ii++) {
            System.out.print("|         ");
         }
         System.out.println("|");
      }
      for(int i = 0; i < 70; i++) {                   // Another for loop for horizontal row barrier
         System.out.print("=");
      }
      System.out.printf("%n");                        // Break
   }

   public static void displayDate(int month, int day) {
      System.out.println("Month: " + month);          // Print month value
      System.out.println("Day: " + day);              // Print day value
   } 
}

Here is the jgrasp run i/o:

 ----jGRASP exec: java MBAssignment2
Please choose a command
    "e" to enter a date and display the corresponding calendar.
    "t" to get todays date and display todays calandar.
    "n" to display the next month.
    "p" to display the previous month.
    "q" to quit program.
e
Desired date(mm/dd)-->
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3720)
    at java.base/java.lang.String.substring(String.java:1909)
    at MBAssignment2.monthFromDate(MBAssignment2.java:103)
    at MBAssignment2.UserDate(MBAssignment2.java:73)
    at MBAssignment2.main(MBAssignment2.java:22)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

0 Answers0