0

So ive created this entire program thinking that i was getting the user to input data but it turns out we need to use args[] to make that happen and now i keep breaking my code trying to fix it. Any help would be greatly appreciated. I also need to fix my formatting still..

Heres the assignment: Write a program Calendar.java that takes two command line arguments m and y and prints out the monthly calendar for the mth month of year y. You should only use conditionals and loops (no arrays as we have not coverd it yet). For example, your output for Calendar 2 2009 should be:

---------------------
    February 2009
---------------------
 Su Mo Tu We Th Fr Sa
  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
---------------------

This is what i do know: During leap years, an extra day is added to the 28 days of February. A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100. e.g: 2000 and 2004 are leap years, but 1900 is not.

Day of Week: Given a month m, a day d, and a year y, the corresponding day of the week is computed as follows:

y0 = y - (14 - m) / 12
x = y0 + y0/4 - y0/100 + y0/400
m0 = m + 12 * ((14 - m) / 12) - 2
d0 = (d + x + (31*m0)/12) mod 7 // 0 for Sunday, ..., 6 for Saturday

Here is my code:

import java.util.Scanner;

public class Calendar {

private static int numDays = 0;
private static int h = 0;
//this method is used to find the given year is leap year or not

public static boolean leap(int year) {
    if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
        return true;
    } else {
        return false;
    }
}
//this method is used to find first day of the year

public static void firstDayOfYear(int year) {
    int month = 13;
    year--;
    h = (1 + (int) (((month + 1) * 26) / 10.0) + year + (int) (year / 4.0) + 6 * (int) (year / 100.0) + (int) (year / 400.0)) % 7;
    String dayName = "";
    switch (h) {
        case 0:
            dayName = "Saturday";
            break;
        case 1:
            dayName = "Sunday";
            break;
        case 2:
            dayName = "Monday";
            break;
        case 3:
            dayName = "Tuesday";
            break;
        case 4:
            dayName = "Wednesday";
            break;
        case 5:
            dayName = "Thursday";
            break;
        default:
            dayName = "Friday";
            break;
    }

}

//this method is used to find first day of the Month
public static void firstDayOfMonth(int month, int year) {
    if (month == 1 || month == 2) {
        month += 12;
        year--;
    }
    h = (1 + (int) (((month + 1) * 26) / 10.0) + year + (int) (year / 4.0) + 6 * (int) (year / 100.0) + (int) (year / 400.0)) % 7;
    String dayName = "";
    switch (h) {
        case 0:
            dayName = "Saturday";
            break;
        case 1:
            dayName = "Sunday";
            break;
        case 2:
            dayName = "Monday";
            break;
        case 3:
            dayName = "Tuesday";
            break;
        case 4:
            dayName = "Wednesday";
            break;
        case 5:
            dayName = "Thursday";
            break;
        default:
            dayName = "Friday";
            break;
    }

}
//this method is used to find first number of days in a month

public static void DaysInMonth(int month, int year) {
    int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (month == 2 && leap(year)) {
        days[month] = 29;
    }
    numDays = days[month];

}
//this method is used to print the calender

public static void calendar(int month, int year) {
    String[] monthNames = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    System.out.println(" " + monthNames[month] + " " + year);
    System.out.println("Su Mo Tu We Th Fr Sa");
    for (int i = 0; i < h - 1; i++) {
        System.out.print(" ");
    }
    for (int i = 1; i <= numDays; i++) {
        System.out.printf("%2d ", i);
        if (((i + h - 1) % 7 == 0) || (i == numDays)) {
            System.out.println();
        }
    }
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter month (1-12): ");
    int month = input.nextInt();
    if (month < 1 || month > 12) {
        System.out.println("Invalid month. Valids inputs are 1-12.");
        System.exit(0);
    }
    System.out.print("Enter year: ");
    int year = input.nextInt();
    if (year < 1753) {
        System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
        System.exit(0);
    }

    firstDayOfYear(year);
    firstDayOfMonth(month, year);
    DaysInMonth(month, year);
    calendar(month, year);
}
}

Here is the error i am getting: Runtime errors:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Calendar.main(Calendar.java:92)

Figure that means that im not allowed to use a scanner and im supposed to use args[]

zlakad
  • 1,246
  • 1
  • 9
  • 15
JediObiJohn
  • 59
  • 2
  • 8
  • 1
    What line is this occurring on in your code? I'm not seeing any errors with the `main` method that would give that. I also copied your code completely in my own IDE and it ran fine. – Spencer Wieczorek Feb 17 '18 at 00:46
  • Possible duplicate of [NoSuchElementException with Java.Util.Scanner](https://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner) – Jérôme Feb 17 '18 at 00:56
  • Please indent your code properly, otherwise it is very hard to read. Use tools provided by your IDE/editor to do so. – Pshemo Feb 17 '18 at 01:12
  • 1
    What do you mean by *no arrays* - you're using arrays: `String[] monthNames`. – zlakad Feb 17 '18 at 01:14
  • Hey @zlackad , you're right i totally shouldnt be. Maybe that's what is kicking it back. What should i do instead? – JediObiJohn Feb 17 '18 at 02:20
  • Did that help @Pshemo – JediObiJohn Feb 17 '18 at 02:21

0 Answers0