title: How to format a calendar based on user input in Java
body: <p>I have an assignment in which the user is asked the start date of the month (0 for Sunday, 1 for Monday etc).</p>
  <p>The following is the code that I have tried:</p>
  <pre><code>import java.util.Scanner;
  
  public class CalendarMonth {
  
      public static void main(String[] args) {
          new CalendarMonth().runApp();
      }
  
      void runApp() {
          Scanner in = new Scanner(System.in);
          System.out.print(&quot;Enter the start day: &quot;);
          int startDay = in.nextInt();
          System.out.print(&quot;Enter the number of days: &quot;);
          int daysMonth = in.nextInt();
  
          System.out.println(&quot;Sun\tMon\tTue\tWed\tThu\tFri\tSat&quot;);
          System.out.println(&quot;---\t---\t---\t---\t---\t---\t---\t&quot;);
  
          //determine starting day with spaces
          startDay%=7;
          //System.out.println(&quot;startDay value is &quot; + startDay);
          for(int i = 0; i&lt;=startDay;i++){
              System.out.print(&quot;X&quot;); //prints a space for each empty day
          }
  
          for(int j = 1; j &lt;= daysMonth; j++){
              if (j &lt; 10){
                  System.out.print(&quot; &quot;);
              }
              if (startDay%7 == 0){
                  System.out.print( j + &quot;\n&quot;);
              }
  
              else{
                  System.out.print(j + &quot;\t&quot;);
              }
              startDay++;
  
          }
          }
  
      }
  
  </code></pre>
  <p>This is the <strong>expected</strong> output:</p>
  <pre><code>Enter the start day: 3
  Enter the number of days: 30
  Sun Mon Tue Wed Thu Fri Sat
  --- --- --- --- --- --- --- 
               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  
  </code></pre>
  <p>This is the actual output:</p>
  <pre><code>Enter the start day: 3
  Enter the number of days: 30
  Sun Mon Tue Wed Thu Fri Sat
  --- --- --- --- --- --- --- 
  XXXX 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  
  </code></pre>
  <p>The XXXX in the first row of the calendar is just a placeholder until I can figure out how to format it. I've been working on this for many hours, I suppose I need help possibly with an explanation if generous. If not, I'll learn as much as I can from the code.</p>
  <p>Thank you in advance.</p>