0

If a user enters string, for example, "Sunday", how do I assign "Sunday" to an int value (such as zero)?

EDIT: Here is the final code for calculating the day of the week after x number of days have elapsed. Not sure if this is the most efficient way.

Thank you for everyone's expertise!

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter today's day: ");
    String day = input.next();
    int todayDay = changeToString(day);
    System.out.println(todayDay);
}

public static int changeToString(String day) {
    int dayName = 0;

    switch (day) {

        case "Monday":
            dayName = 1;
            break;
        case "Tuesday":
            dayName = 2;
            break;
        case "Wednesday":
            dayName = 3;
        case "Thursday":
            dayName = 4;
        case "Friday":
            dayName = 5;
        case "Saturday":
            dayName = 6;

    }

    int dayselapsed = input.nextInt();
    int futureday = (dayName + dayselapsed) % 7; 

    switch (futureday) {

        case 0: System.out.println("In " + dayselapsed + " days, it will be Sunday");
            break;
        case 1: System.out.println("In " + dayselapsed + " days, it will be Monday"); 
            break;
        case 2: System.out.println("In " + dayselapsed + " days, it will be Tuesday"); 
            break;
        case 3: System.out.println("In " + dayselapsed + " days, it will be Wednesday"); 
            break;
        case 4: System.out.println("In " + dayselapsed + " days, it will be Thursday"); 
            break;
        case 5: System.out.println("In " + dayselapsed + " days, it will be Friday"); 
            break;
        case 6: System.out.println("In " + dayselapsed + " days, it will be Saturday"); 
            break;
}
    return 0;

    }

}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • Is your an example just an example? or is it your question? There are a variety of strategies that can be used, depending on your exact situation, localization, whether it's date related, math related etc. – Ryan Leach Jun 28 '17 at 02:33
  • if using java 8 check this https://stackoverflow.com/questions/18232340/convert-string-to-day-of-week-not-exact-date/44792784#44792784 – Deepak Kumar Jun 28 '17 at 02:42
  • I want to assign an int value to each day of the week the user enters (for example, if the user enters "Sunday", assign that to an int value of my choice - zero). So that I can perform a specific calculation with zero, only if the user inputs "Sunday". I would perform the same calculation, but with the int value 1, if the user inputs "Monday". I want to know how to assign "Monday" to 1, "Tues" to 2, etc. – Sandra Lee Jun 28 '17 at 02:47
  • I can't answer this now that Tim has closed it, but the simplest way is to use the names built into the JDK. Then, you can do it in just two lines. `List dayNames = Arrays.asList(new DateFormatSymbols().getWeekdays()); return dayNames.indexOf(day) - 1;` – Dawood ibn Kareem Jun 28 '17 at 03:55

1 Answers1

1

You can use this method

public class Day {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter today's day: ");
        String day = input.next();
        int todayDay = changeToString(day);
        System.out.println(todayDay);
    }

    public static int changeToString(String day) {
        int dayName = 0;

        switch (day) {
            case "Monday":
                dayName = 1;
                break;
            case "Tuesday":
                dayName = 2;
                break;
             ......
        }

        return dayName;
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135