1

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;
    }
2
  • Initalialize the variable statePosition. 0 would work just fine. Commented Nov 30, 2013 at 19:18
  • String state= null; int statePosition=-1; Commented Nov 30, 2013 at 19:19

7 Answers 7

1

Can you use

 int statePosition = -1;

That would return -1 if it's not found. The error means you didn't assign a value to statePosition.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to initialize your variable int statePosition; like this:

int statePosition =-1;

Also initialize your String state; like this:

String state = null;

Comments

0

Like the exception states, you have to initialize the int statePosition:

int statePosition = null;

or

int statePosition = 0;

... you know what I mean?

Comments

0

you need to initialize both variables

String state= null;
int statePosition=-1;

Comments

0

The problem here is that you never instantiate the statePosition variable but you are always returning it.

Try give statePosition a value (like others said -1).

Also try to do this with a while (instead of a for statement) which breaks if your statePosition was found.

Comments

0

Your program is prone to encounter an error in one of it variables prior to their use further in the code. I would suggest that every time you code, you should put the variables to initial state rather than leaving them with a null value.

String state= null;
int statePosition=-1;

Put this in the code to have your problems solved.

Comments

0

You might want to change from using int to Integer. Integer is an object so it can be null. This way you can check to see if it has been set before performing any further actions.

static final int NUM_STATES = 50;

public static Integer askState(String[] stateNames) {
    Scanner keyboard = new Scanner(System.in);
    String state;
    Integer 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();
    }
    if (statePosition == null) {
        throw new Exception("State Position Not Set :(");
    }
    return statePosition;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.