[Solved]Lab Use Learned Searching Array Find Exact Match Complete Partially Prewritten Java Progra Q37172639
In this lab, you use what you have learned about searching anarray to find an exact match to complete a partially prewrittenJava program. The program uses an array that contains valid namesfor 10 cities in Michigan. You ask the user of the program to entera city name; your program then searches the array for that cityname. If it is not found, the program should print a message thatinforms the user the city name is not found in the list of validcities in Michigan. The data file provided for this lab includesthe input statements and the necessary variable declarations. Youneed to use a loop to examine all the items in the array and testfor a match. You also need to set a flag if there is a match, andthen test the flag variable to determine if you should print the”Not a city in Michigan” message. Comments in the code tell youwhere to write your statements. Instructions Study the prewrittencode to make sure you understand it. Write a loop statement thatexamines the names of cities stored in the array. Write code thattests for a match. Write code that, when appropriate, prints themessage: “Not a city in Michigan.”. Execute the program using thefollowing as input: Chicago Brooklyn Watervliet Acme
// MichiganCities.java – This program prints a message forinvalid cities in Michigan.
// Input: Interactive.
// Output: Error message or nothing.
import java.util.Scanner;
public class MichiganCities
{
public static void main(String args[]) throwsException
{
// Declare variables.
StringinCity; // name of city to look up in array.
// Initialized array of cities inMichigan.
String citiesInMichigan[] = {“Acme”,”Albion”, “Detroit”, “Watervliet”, “Coloma”, “Saginaw”, “Richland”,”Glenn”, “Midland”, “Brooklyn”};
boolean foundIt = false; // Flagvariable.
intx; //Loop control variable.
// Get user input.
Scanner input = newScanner(System.in);
System.out.println(“Enter the name of thecity: “);
inCity = input.nextLine();
// Write your loop here.
// Write your teststatement here to see if there is
// a match. Setthe flag to true if city is found.
}
// Test to see if city was not foundto determine if
// “Not a city in Michigan” messageshould be printed.
System.exit(0);
} // End of main() method.
} // End of MichiganCities class.
How would i do this?
Expert Answer
Answer to In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewr… . . .
OR

