-3

I need to import the enum values that is ,days, which i have created in separate enum under same package. What i am asking is that how to import the enum values into main program using Scanner..

package com.huo.kop;

import java.util.Scanner;

public class WeekDay {


    public static void main(String[] args) {

        Scanner sc=new Scanner (System.in);
        System.out.println("Enter the day(only three letters with first letter being Capital : ");
        String dayWeek=sc.next();
        if(dayWeek.equals("Sat") || dayWeek.equals("Sun"))
        {System.out.println("Yay, Its a Weekend!!!");   
        }
        else
        {System.out.println("Buah..Its still WeekDay!! Need to Work");}
happyvirus
  • 279
  • 3
  • 21
Sentōki
  • 1
  • 1
  • 1
    Please fix the formatting, add the code for the enum and define what you mean by "import the enum values". – reto Nov 20 '14 at 06:57
  • 2
    What does the use of strings have to do with enums? – Unihedron Nov 20 '14 at 06:57
  • 2
    Does [the question "Java - Convert String to enum"](http://stackoverflow.com/questions/604424/java-convert-string-to-enum) help? – reto Nov 20 '14 at 06:58

1 Answers1

0

You will have to convert string into your enum manually. For example:

public static Weekday getWeakdayForString(String s)
{
    switch (s) {
        case "monday": return Weekday.MONDAY;
        case "tuesday": return Weekday.TUESDAY;
        ...
        case "sunday": return Weekday.SUNDAY;
        default: throw new IllegalArgumentException();
    }
}

In Java 8, there is pre-defined DayOfWeek enum which also provides methods to get the display name of the day of the week, so you can do:

public static DayOfWeek getDayOfWeakForString(String s)
{
    for (DayOfWeek dow : EnumSet.allOf(DayOfWeek.class)) {
        if (dow.getDisplayName(TextStyle.FULL, Locale.getDefault()).equals(s)) {
            return dow;
        }
    } 
    throw new IllegalArgumentException();
} 

When reading from the scanner, you then do:

DayOfWeek dow = getDayOfWeekForString(sc.next());
Hoopje
  • 12,677
  • 8
  • 34
  • 50