Pretty new to Java, how to insert user-inputted string to direct a reference?
(In java,) This may or may not be possible, I assume it is because it
seems to short-cutty/easy to not exist. Basically what I want to do is
have the user insert the name of a state, then it add one to that state's
counter, which is stored in a different class. I want to do this without
creating 50 if/else statements. Here's pseudocode which represents how I
would expect it to be done. (this code would be encapsuled in a while loop
in mainclass.java and the counters and state names are in a class named
state.java.)
Scanner userstate = new Scanner(System.in);
String statename = userstate.nextLine();
state.(statename).counter++;
in state.java:
public state(int counter){
}
public static state Alabama = new state(0);
For time's sake, I hope there is a shortcut similar to the above.
No comments:
Post a Comment