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. Study the prewritten code to make sure you understand it. Write a loop statement that examines the names of cities stored in the array. Write code that tests for a match. Write code that, when appropriate, prints the message: "Not a city in Michigan.". Execute the program using the following as input: Chicago Brooklyn Watervliet Acme Code: // 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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

  1. Study the prewritten code to make sure you understand it.
  2. Write a loop statement that examines the names of cities stored in the array.
  3. Write code that tests for a match.
  4. Write code that, when appropriate, prints the message: "Not a city in Michigan.".
  5. Execute the program using the following as input:

Chicago

Brooklyn

Watervliet

Acme

Code:

// 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 5 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education