Question
Hello, I need this program to check and array to see if the user input is held within. I did a for loop which I
Hello, I need this program to check and array to see if the user input is held within. I did a for loop which I would think would do what is needed, and i have the return statements in there. but it will not run, what is needed for this to be able to run?
// MichiganCities.java - This program prints a message for invalid cities in Michigan. // Input: Interactive. // Output: Error message or nothing.
import java.util.Scanner;
public class MichiganCities { public static void main(String args[]) throws Exception { // Declare variables. String inCity; // name of city to look up in array. // Initialized array of cities in Michigan. String citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"}; boolean foundIt = false; // Flag variable. int x; // Loop control variable.
// Get user input. Scanner input = new Scanner(System.in); System.out.println("Enter the name of the city: "+(x+1)); inCity = input.nextLine(); // Write your loop here. for (int x = 0; x < citiesInMichigan.length; x++) { if(incity == citiesInMichigan[x]); { foundIt = true; } } System.out.println("city entered is a city in Michigan"); else System.out.println("city entered is not a city in Michigan"); } System.exit(0);
} // End of main() method. } // End of MichiganCities class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started