subject
Computers and Technology, 14.03.2020 06:30 ava108

Searching an Array for an Exact Match

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

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
// 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.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:00
Which of the following physical laws can make the flow of water seem more realistic? a. motion b. gravity c. fluid dynamics d. thermodynamics
Answers: 2
question
Computers and Technology, 24.06.2019 04:30
How do you share someone else’s tweet with your own twitter followers?
Answers: 1
question
Computers and Technology, 25.06.2019 00:00
When pasting an existing chart into a word document, you can choose to using the paste options button. a. paste the chart as a picture b. embed the chart c. add the chart while keeping the destination formatting intact d. insert the chart while keeping the source formatting intact
Answers: 1
question
Computers and Technology, 25.06.2019 05:10
Write a program that asks for 'name' from the user and then asks for a number and stores the two in a dictionary (called 'the_dict') as key-value pair. the program then asks if the user wants to enter more data (more data (y/n)? ) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list. note: ignore the case where the name is already in the dictionary. example: name: pranshu number: 517-244-2426 more data (y/n)? y name: rich number: 517-842-5425 more data (y/n)? y name: alireza number: 517-432-5224 more data (y/n)? n [('alireza', '517-432-5224'), ('pranshu', '517-244-2426'), ('rich', '517-842-5425')]
Answers: 3
You know the right answer?
Searching an Array for an Exact Match

Summary

In this lab, you use what yo...
Questions
question
Social Studies, 23.07.2019 07:30
question
Mathematics, 23.07.2019 07:30
question
History, 23.07.2019 07:30
Questions on the website: 13722363