In: Computer Science
Searching an Array for an Exact Match in Java
Summary
In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten Java program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in the list of valid cities in Michigan. The data file provided for this lab includes the input statements and the necessary variable declarations. You need to use a loop to examine all the items in the array and test for a match. You also need to set a flag if there is a match, and then test the flag variable to determine if you should print the "Not a city in Michigan" message. Comments in the code tell you where to write your statements.
Instructions
Chicago Brooklyn Watervliet Acme
Grading
When you have completed your program, click the Submit button to record your score.
// 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: ");
inCity = input.nextLine();
// Write your loop here.
// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
}
// Test to see if city was not found to determine if
// "Not a city in Michigan" message should be printed.
System.exit(0);
} // End of main() method.
} // End of MichiganCities class.
CODE:
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: ");
inCity = input.nextLine();
// Write your loop here.
boolean flag = false;
for (x = 0; x < citiesInMichigan.length; x++) {
if (citiesInMichigan[x].equals(inCity)) {
foundIt = true;
break;
}
}
// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
if (foundIt == false)
System.out.println("Error: City not found");
// Test to see if city was not found to determine if
// "Not a city in Michigan" message should be printed.
System.exit(0);
} // End of main() method.
} // End of MichiganCities class.
OUTPUT:
Please upvote if you like my answer and comment below if you have any queries or need any further explanation.