-1

So what I need to do is calculate the time of a rental from 0900 to 1600 Monday to Friday and store this

I have tried to remove the calculation for one day, but still does not compile in the correct format.

public class Time {

    // Variables

    static Scanner kbinput = new Scanner(System.in);

    private static boolean error = false;

    private static String daysoftheweek;            //  Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;

    DateFormat sdf = new SimpleDateFormat("HH");
    // Keyboard for user input

    private int time;

    public static void main(String[] args) throws ParseException {

        System.out.println("Please enter day of your rental");
        daysoftheweek = kbinput.nextLine();
        System.out.println("Please enter day of your return");

        do {
            daysoftheweek = kbinput.nextLine();
            switch (daysoftheweek.toUpperCase()) {
                case "Monday":
                    error = false;
                    break;
                case "Tuesday":
                    error = false;
                    break;
                case "Wednesday":
                    error = false;
                    break;
                case "Thursday":
                    error = false;
                    break;
                case "Friday":
                    break;
                case "Saturday":
                default:
                case "Sunday":
            }

        } while (error);

        System.out.println("Please enter your start time (hh) ");

        String time = kbinput.nextLine();

        System.out.println();
        System.out.print("Enter finish time (hh) ");

        String time2 = kbinput.nextLine();

        DateFormat sdf = new SimpleDateFormat("hh");

        Date d1 = sdf.parse(time);
        System.out.println("Rental Time: " + sdf.format(d1));
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
redstar
  • 1
  • 2
  • Take a good look at your cases in the switch – geco17 Jan 08 '19 at 21:34
  • Edit your code and make sure all is included and also clarify your question – Joakim Danielson Jan 08 '19 at 21:35
  • This code compiles – azro Jan 08 '19 at 21:35
  • 1
    you immediately overwrite `daysoftheweek` without using it in the switch and then you upcase a string and then compare it to all mixed case strings they will never match. – mavriksc Jan 08 '19 at 21:39
  • java 8? ..have a look at https://docs.oracle.com/javase/tutorial/datetime/iso/period.html ... (does your calculation consider "time zones" (summer-winter change) .."leap years/seconds"?;) – xerx593 Jan 08 '19 at 21:39
  • @xerx593 while those are considerations for some not in this case. the parameters for how much time and what days are available are limited. no leaps or time zones to worry about. and while summer-winter could be an issue we don't get the date only a day so no way to calc that anyway. no need to use any java time/cal stuff for this at all. except maybe the enums mentioned. – mavriksc Jan 08 '19 at 21:46
  • You may want to use two different variables for the rental day and the return day. Currently, you're storing them both in the same variable, which means that the second one overwrites the first. – Dawood ibn Kareem Jan 08 '19 at 21:55
  • Also, you have two different variables called `sdf` - one is local in `main` and the other is a field. This is certain to cause confusion. You may only need one of them (and it should probably be `"HH"`, not `"hh"`). Or you may not need that at all, since all you're doing is parsing a number between 9 and 16. – Dawood ibn Kareem Jan 08 '19 at 21:57

1 Answers1

0

String comparisons in Java are case-sensitive. In the line:

switch (daysoftheweek.toUpperCase()) {

You convert the user's input to an all uppercase string, i.e. "Monday" becomes "MONDAY". This is never going to match any of the cases in the switch block.

A short-term fix would be to replace all the cases with their uppercase versions. A more robust approach would involve using the built in Java enumeration for days of the week.