I typed something meant for the console into my code, and I can't find where I messed up

Viewed 81

I was working on my assignment testing out to make sure it all worked, I accidentally typed an input meant for the console into my code and the output is no longer correct. Here is the code:

import java.util.*;

public class JTCalendar {
    //This defines how big the calendar is
    public static final int Width = 5;
       
       public static void drawMonth(int month,int day) {
          
          //Ascii art of a robot man
          System.out.println("    /\\     /\\");
          System.out.println("   /  \\   /  \\");
          System.out.println(" ---------------");
          System.out.println("(| (o)     (o)  |)");
          System.out.println(" |    wwww      |");
          System.out.println(" ----------------");
          
          //This finds the center
          for(int i = 1; i <= 1; i++) {
             for(int j = 1; j <= (Width * 7) / 2; j++) {
             System.out.print(" ");
             }   
             System.out.print(month);
             for(int d = 1; d <= (Width * 7) / 2 ; d++) {
             System.out.print(" ");
             }
             System.out.println();

I believe the error got put somewhere along this area as this is the area that has a messed up output

      }
      //Size calculator
   for(int g =1; g <= Width * 7; g++) {
      System.out.print("=");
   }
   System.out.println();
   drawRow(5); 
   displayDate(month,day); 
   }
      //Start of the rows
      public static void drawRow(int row) {
      int day = 1;
         for(int i = 0; i < row; i++) {
            
            System.out.print("|");
            for(int a = 1; a <= 7; a++) {
               
               int length=String.valueOf(day).length();
               for(int j = 1; j < Width - length; j++) {
                  System.out.print(" ");
               }
                  
            System.out.print(day);  
            day++;
            System.out.print("|");
            }  
         System.out.println();
         for(int t = 1; t < (Width /2); t++) {
            System.out.print("|");
            for(int h = 1; h<=7; h++) {
         
               for(int f = 1; f <= Width - 1; f++) {
                     System.out.print(" ");
               }
            System.out.print("|");
            }
         }
         for(int e = 1; e <= Width * 7; e++) {
            System.out.print("=");
         }
      System.out.println();
      }
      //End of the rows
   }
   //Below displays the date
   public static void displayDate(int month, int day) {
      System.out.println("Month: " + month + "\nDay: " + day);
   }
   //End display
   //Below seperates month and day into two ints
   public static int monthFromDate(String date) {
      String tokens[] = date.split("/");
      return Integer.parseInt(tokens[0]);
   }

I'm confident anything below this is proper but I'm still including it

public static int dayFromDate(String date) {
      String tokens[] = date.split("/");
      return Integer.parseInt(tokens[1]);
   }
   //End seperation of month and day
   
   public static void main(String[] args) {
      //Main scanner console
      Scanner console = new Scanner(System.in);
      System.out.print("What date would you like to look at? (mm/dd): ");
      String date = console.next();
      int day = dayFromDate(date);
      int month = monthFromDate(date);      
      drawMonth(month, day);
      //This draws the ascii calendar
      
      Calendar cal = Calendar.getInstance();
         day = cal.get(Calendar.DATE);
         month = cal.get(Calendar.MONTH) + 1;
         System.out.println("\nToday is: " + month + "/" + day + "\n");
         drawMonth(month,day);
      //This draws the calandar for the current date
   }
}

Bad output:

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |===================================
Month: 2
Day: 25

Today is: 2/13

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |===================================
Month: 2
Day: 13

Good output:

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |
===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |
===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |
===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |
===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |
===================================
Month: 2
Day: 25

Today is: 2/13

    /\     /\
   /  \   /  \
 ---------------
(| (o)     (o)  |)
 |    wwww      |
 ----------------
                 2                 
===================================
|   1|   2|   3|   4|   5|   6|   7|
|    |    |    |    |    |    |    |
===================================
|   8|   9|  10|  11|  12|  13|  14|
|    |    |    |    |    |    |    |
===================================
|  15|  16|  17|  18|  19|  20|  21|
|    |    |    |    |    |    |    |
===================================
|  22|  23|  24|  25|  26|  27|  28|
|    |    |    |    |    |    |    |
===================================
|  29|  30|  31|  32|  33|  34|  35|
|    |    |    |    |    |    |    |
===================================
Month: 2
Day: 13
2 Answers

You need to add a System.out.println(); after drawing each row:

         System.out.println();
         for(int t = 1; t < (Width /2); t++) {
            System.out.print("|");
            for(int h = 1; h<=7; h++) {
         
               for(int f = 1; f <= Width - 1; f++) {
                     System.out.print(" ");
               }
            System.out.print("|");
            }
         }
        
         System.out.println(); // <== ADDED PRINTLN HERE

         for(int e = 1; e <= Width * 7; e++) {
            System.out.print("=");
         }
      System.out.println()

The problem is in the drawRow(int row) method , after you print out the '|' character using the for loop then it's not starting in a new line to print out the '=' characters ,you have to insert one System.out.println() statment to make it start in next line

your Code

 for(int t = 1; t < (Width /2); t++) {
        System.out.print("|");
        for(int h = 1; h<=7; h++) {
     
           for(int f = 1; f <= Width - 1; f++) {
                 System.out.print(" ");
           }
        System.out.print("|");
        }
     }

Newer Code

 for(int t = 1; t < (Width /2); t++) {
        System.out.print("|");
        for(int h = 1; h<=7; h++) {
     
           for(int f = 1; f <= Width - 1; f++) {
                 System.out.print(" ");
           }
        System.out.print("|");
        }
        
         System.out.println("");  //<------------ the only change you need to make
      }
Related