Sorry, but you do it all wrong.
First, you explain things wrong. You say you want "dayNum to be equal to the value of the string", but then in example "dayNum should = 1" - where 1 is not "the value of the string". I get what you mean, but the explanation is phrased incorrectly. Though it's not very important.
What is important is that you expect a certain level of metaprogramming that Java doesn't have. Namely, you want to operate on names of local variables. The program should read value of local variable int monday
when some other local variable has value 'monday'. Java doesn't have that. You could do that with reflection if int monday
was a field of a class and not a local variable, but here comes another problem - you're doing it all wrong.
There are number of ways you can implement this, but what you certainly shouldn't do is have variables whose names are names of days of weeks.
One way to do this is to have an array of Strings that contains names of days (lowercased):
String[] days = new String[] { 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' }
Then you could trim and lowercase your input - curDay = console.next().trim().toLowerCase()
- and then loop over array to find the matching value, and the index at which value matches would be the answer:
int dayNum = -1;
for(int i=0; i<days.length; i++) {
if(days[i].equals(curDay) {
dayNum = i;
break;
}
}
Another solution that would be simpler to implement, faster to work, but perhaps harder to understand is to use a Map (and the hardest part to understand is the internals of how Map implementations work - but you do not absolutely need this to use them):
Map<String, Integer> daysOfWeek = new HashMap<String, Integer>();
daysOfWeek.put("sunday", 0);
...
daysOfWeek.put("saturday", 6);
String curDay = console.next().trim().toLowerCase();
int dayNum = daysOfWeek.get(curDay); // Will throw null-pointer exception if value of curDay is not in the map.
Also the right way to do this would be to use Java APIs for manipulating date - the java.util.Calendar
(well, rather java.text.DateFormatSymbols
) - to get the names of days of week for a specific locale. See this answer on how to do that:
In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system's default Locale (language)