Very new to Java, I am in an intro class in college and doing a project. I am trying to make a method that searches a String array for an inputted state and returns the index. If the user enters a query that is not in the array, I would like it to ask for a new state to search. My exception is saying "variable statePosition may not have been initialized." Below is the code.
Thank you in advance!
static final int NUM_STATES = 50;
public static int askState(String[] stateNames) {
Scanner keyboard = new Scanner(System.in);
String state;
int statePosition;
System.out.println("Please enter a state that you would like to search:");
state = keyboard.next();
{
for (int i = 0; i < NUM_STATES; i++) {
if (state.equals(stateNames[i])) {
statePosition = i;
} else {
System.out.println("Please enter a valid state:");
}
state = keyboard.next();
}
return statePosition;
}