0

I'm learning java and I got stuck at a question where I need to enter the days of the week into an enum. I then need to put several responses into a switch case statement and have a user enter the day of the week into a JOptionPane and get the program to output the appropriate response.

This is my Enum:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY; 
}

This is my attempt at getting the correct response:

public class enumDayMood {
    Day day;
    
    public static void main(String[] args){
        String day = JOptionPane.showInputDialog("Enter the day of the week.");
    }
    
    public void telDayMood(){
        
        Day day = Day.MONDAY;
        
        switch(day){
            case MONDAY:
                JOptionPane.showMessageDialog(null,"Mondays are bad.");
                break;
            case FRIDAY:
                JOptionPane.showMessageDialog(null,"Fridays are better.");
                break;
            case SATURDAY: case SUNDAY:
                JOptionPane.showMessageDialog(null,"Weekends are best");
                break;
            default:
                JOptionPane.showMessageDialog(null,"Midweek days are so-so.");
                break;
        }
    }
    
}

I was able to output every day of the week individually but I can't seem to get the user input to work. I've tried multiple methods but none seem to work.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

1 Answers1

1

I suggest using method showOptionDialog rather than showInputDialog. Make each day of the week an option. Then the user just needs to click on the relevant button and that would save him some work, no? Consider the following:

import javax.swing.JOptionPane;

public class WeekDays {
    public enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
    }

    public static void main(String[] args) {
        int index = JOptionPane.showOptionDialog(null,
                                                 "Enter the day of the week.",
                                                 "Moody",
                                                 JOptionPane.DEFAULT_OPTION,
                                                 JOptionPane.QUESTION_MESSAGE,
                                                 null,
                                                 Day.values(),
                                                 Day.MONDAY);
        Day selection = Day.values()[index];
        switch (selection) {
            case MONDAY:
                JOptionPane.showMessageDialog(null,"Mondays are bad.");
                break;
            case FRIDAY:
                JOptionPane.showMessageDialog(null,"Fridays are better.");
                break;
            case SATURDAY: case SUNDAY:
                JOptionPane.showMessageDialog(null,"Weekends are best");
                break;
            default:
                JOptionPane.showMessageDialog(null,"Midweek days are so-so.");
                break;
        }
        System.exit(0);
    }
}

This is how it looks when I run it.

screen capture of running app

Abra
  • 19,142
  • 7
  • 29
  • 41