4

First off, I am a beginner programmer and have only about 7 weeks of programming experience. Second this is for a homework assignment. Here is where I am stuck.

String curDay;
String curDaylow;
int sunday;
int monday;
int tuesday;
int wednesday;
int thursday;
int friday;
int saturday;
sunday = 0;
monday = 1;
tuesday = 2;
wednesday = 3;
thursday = 4;
friday = 5;
saturday = 6;
int dayNum;

curDay = console.next();
curDaylow = curDay.toLowerCase();
dayNum = valueOf.curDaylow;

What i am trying to do is get the variable dayNum to be equal to the value of the string. Example. if the user enters Monday, the program reduces it to all lowercase monday, and then dayNum should = 1. I already have Sun-Sat declare as value INT and have each one instanced starting with SUN=0 and moving down the line to SAT = 6.

As this is homework I do not expect someone to do this for me, but perhaps nudge me to where I can learn how to accomplish this. Or perhaps tell me what this operation would be called so I know to research it. Thanks.

daelon_rax
  • 41
  • 3

3 Answers3

2
enum Day {
    sunday(0),monday(1),tuesday(2),wednesday(3),thursday(4),
    friday(5),saturday(6);

    private final int value;

    Day(int value) {
        this.value = value;
    }

    int getValue() {
        return value;
    }
}

Now you can use:

Day m = Day.valueOf(curDay.toLowerCase());
int dayNum = m.getValue();
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • 1
    Enums already have method which returns their position: `ordinal()`. – Pshemo Jan 29 '16 at 00:41
  • 1
    I think it's not a good practice, but it could work. Read this: http://stackoverflow.com/questions/1574580/is-it-bad-practice-to-use-an-enums-ordinal-value-to-index-an-array-in-java, besides it wouldn't work in a more general case, where numbering doesn't start from 0 (I for example would understand why someone would like to have monday=1, ... sunday=7) – Gavriel Jan 29 '16 at 00:43
  • 1
    True, it has its disadvantages like it makes changing position of elements harder, but in current case your `getValue()` method acts exactly the same as `ordinal()` which is why I mentioned it. I am not saying your solution is wrong (sorry if that wasn't clear in my first comment). I was just trying to point out fact. – Pshemo Jan 29 '16 at 00:50
  • @Pshemo, that' fine, I understood your intention, that's why I wrote that it could work in this case. – Gavriel Jan 29 '16 at 00:57
2

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)

Community
  • 1
  • 1
mvmn
  • 3,717
  • 27
  • 30
0

Buddy you are doing few blunders, like you have defined int variables as sunday,monday,tuesday...... and when user enters a string value, you are expecting to extract the integer value from that string.Not possible.What you can do is use switch case and if you are using java 1.7, you can pass your input string into that switch and then in case you can define case "sunday": dayNum=0; break; Similarly enter all days in case. Hope it helps.

int dayNum;
    String curDay;
    String curDaylow;
    curDay = console.next();
    curDaylow = curDay.toLowerCase();
    switch (curDaylow) {
    case "sunday":
        dayNum = 0;
        break;
    case "monday":
        dayNum = 1;
        break;
    // Similarly write all cases for remaining days
    }
abdulrafique
  • 304
  • 1
  • 11
  • 1
    @ abdulrafique, & @yshavit, thanks guys, looks like the switch is the way to go. Yes quite a few blunders. I am very much a beginner to programming, and Java is my first language. I will give the switch a shot and see what happens. Thanks again for the nudge and fast response. It is greatly appreciated. – daelon_rax Jan 29 '16 at 00:40
  • going this route, you can eliminate the `String` variables and do `switch (console.next().toLowerCase())` – Calvin P. Jan 29 '16 at 00:43
  • @CalvinP. While that's certainly true, I find that it's a good habit to put the thing you're switching on into a variable if it's something that can't easily be retrieved again (which something like `console.next()` can't), for debugging. The one extra line of vertical space isn't much. That said, to the answerer (and also the OP) -- you can (and should) consolidate the variable declaration and assignment: `String curDay = console.next()`, and similarly for `curDayLow`. – yshavit Jan 29 '16 at 00:58