is there a "tidier" way of writing this code? I'm a stickler for simplicity and this seems a bit too repetitive for me. (I'm changing the days to numbers so that I can use them in their own if statements, if anyone was wondering) Any suggestions?
Scanner scanText = new Scanner(System.in);
System.out.print("Enter Day: ");
String weekday = scanText.nextLine();
int day = 0;
if (weekday.equalsIgnorCase("Monday"))
day = 1;
else if (weekday.equalsIgnorCase("Tuesday"))
day = 2;
else if (weekday.equalsIgnorCase("Wednesday"))
day = 3;
else if (weekday.equalsIgnorCase("Thursday"))
day = 4;
else if (weekday.equalsIgnorCase("Friday"))
day = 5;
else if (weekday.equalsIgnorCase("Saturday"))
day = 6;
else if (weekday.equalsIgnorCase("Sunday"))
day = 7;
else {
System.out.print("Error! Invalid day: ");
weekday = scanText.nextLine();
}
If you are not using JDK 1.8, this code may help you:
List<String> strDays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday", "Sunday" );
String weekday = scanText.nextLine();
int day = 0;
if(strDays.contains(weekday)) {
day = strDays.indexOf(weekday) + 1;
} else {
System.out.print("Error! Invalid day: ");
weekday = scanText.nextLine();
}
import java.io.*;
import java.text.*;
import java.util.*;
public class DayofWeekExample {
public static int getIntDayOfWeek(String dayOfWeek)
{
try
{
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("EEE");
date = (Date)formatter.parse(dayOfWeek);
GregorianCalendar g = new GregorianCalendar();
g.setTime(date);
return g.get(Calendar.DAY_OF_WEEK);
}
catch (ParseException e)
{
System.out.println("Exception :"+e);
}
return 0;
}
public static void main(String[] args) throws IOException {
// Beginnig day is Sunday
Scanner scanText = new Scanner(System.in);
System.out.print("Enter Day: ");
String weekday = scanText.nextLine();
System.out.println("-> " + getIntDayOfWeek(weekday));
}
}
The first day of the week is derived from the current locale. If you don't set the locale of the calendar ( Calendar.getInstance(Locale) , or new GregorianCalendar(Locale) ), it will use the system's default.
Since you are basically mapping a String
value to an int
value, I think, its a typical use case of Map
. You can write like below ( one disadvantage being you have to initialize the Map
but that can be encapsulated for better readability)
Scanner scanText = new Scanner(System.in);
System.out.print("Enter Day: ");
String weekday = scanText.nextLine();
int day = 0;
Map<String,Integer> day_of_week=new HashMap<String,Integer>();
day_of_week.put("MONDAY", 1);
day_of_week.put("TUESDAY", 2);
day_of_week.put("WEDNESDAY", 3);
day_of_week.put("THURSDAY", 4);
day_of_week.put("FRIDAY", 5);
day_of_week.put("SATURDAY", 6);
day_of_week.put("SUNDAY", 7);
if(day_of_week.containsKey(weekday.toUpperCase())){
day = day_of_week.get(weekday.toUpperCase()).intValue();
}else{
System.out.print("Error! Invalid day: ");
weekday = scanText.nextLine();
}
I am sure you must have thought of going to comparison part again after reading value from else
block.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.