Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop error in java
    primarykey
    data
    text
    <p>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. At the moment I am stuck in the loop. I know I am entering a valid state, but it keeps asking for a valid state and will out let me out of the loop.</p> <p>Thank you.</p> <pre><code>public static void main(String[] args) throws FileNotFoundException { String[] stateNames = importFile("stateNames.txt"); String[] nicknames = importFile("nicknames.txt"); String[] populations = importFile("population.txt"); String[] flowers = importFile("flowers.txt"); String[] Capital = importFile("capitals.txt"); int index = askState(stateNames); System.out.println("The index of your state is"+ index); } static final int NUM_STATES = 50; public static String[] importFile(String fileName) throws FileNotFoundException { String[] array = new String[NUM_STATES]; File inputFile = new File(fileName); Scanner scanner = new Scanner(inputFile); for (int i = 0; i &lt; NUM_STATES; i++) { array[i] = scanner.nextLine(); } return array; } public static int askState(String[] stateNames) { Scanner keyboard = new Scanner(System.in); String state; int statePosition = -1; System.out.println("Please enter a state that you would like to search:"); state = keyboard.nextLine(); { for (int i = 0; i &lt; NUM_STATES; i++) { if (state.equals(stateNames[i])) statePosition = i; else { System.out.println("Please enter a valid state:"); state = keyboard.next(); }} return statePosition; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload