Could not find or load main class Main exit status 1
mariobucio (0)

https://repl.it/@mariobucio/The-Clock-Program-1

I'm taking a java class and having trouble figuring out why this code doesn't compile correctly. The code is straight from the book so I think I might be doing something wrong? Any help would be appreciated.

You are viewing a single comment. View All
heyitsmarcus (287)

When you run a Java console program, you have to have at least a class Main inside of your program for it to run. So, what I did was renamed the main function inside of your Clock class to be a clock function, and then inside of a new public class Main made the main function that runs with every program and called Clock.clock(); to run the clock program.

Here is the repl code: https://repl.it/@heyitsmarcus/The-Clock-Program-1

import java.util.*;

public class Main {
  public static void main(String[] arguments) { 
    //run the clock program automatically upon execution
    Clock.clock();
  }
}

class Clock {
  public static void clock() {
    // get current time and date
    Calendar now = Calendar.getInstance();
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int month = now.get(Calendar.MONTH) + 1;
    int day = now.get(Calendar.DAY_OF_MONTH);
    int year = now.get(Calendar.YEAR);

    // display greeting
    if (hour < 12) {
      System.out.println("Good morning.\n");
    } else if (hour < 17) {
      System.out.println("Good afternoon.\n");
    } else {
      System.out.println("Good evening.\n");
    }

    // begin time message by showing the minutes
    System.out.print("It's");
    if (minute != 0) {
      System.out.print(" " + minute + " ");
      System.out.print((minute != 1) ? "minutes" : "minute");
      System.out.print(" past");
    }

    // display the hour
    System.out.print(" ");
    System.out.print((hour > 12) ? (hour - 12) : hour);
    System.out.print(" o'clock on ");

    // display the name of the month
    switch (month) {
    case 1:
      System.out.print("January");
      break;
    case 2:
      System.out.print("February");
      break;
    case 3:
      System.out.print("March");
      break;
    case 4:
      System.out.print("April");
      break;
    case 5:
      System.out.print("May");
      break;
    case 6:
      System.out.print("June");
      break;
    case 7:
      System.out.print("July");
      break;
    case 8:
      System.out.print("August");
      break;
    case 9:
      System.out.print("September");
      break;
    case 10:
      System.out.print("October");
      break;
    case 11:
      System.out.print("November");
      break;
    case 12:
      System.out.print("December");
    }

    // display the date and year
    System.out.println(" " + day + ", " + year + ".");
  }
}