Examine all the items in the array


Discussion:

Q: The program uses an array that contains valid names for 10 cities in the Illinois. 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 that the city name is not found in the list of valid cities in Illinois.

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 Illinois" message. Comments in the code tell you where to write your statements. You can use the Mail Order Program in this chapter guide.

- Write a loop statement that examines the names of cities stored in the array.

- Write a code that tests for a match.

- Write code that, when appropriate, prints the message: Not a city in Illinois.

- Execute the program using the following as input:

o Peoria
o Kankakee
o Austin
o Boise

// IllinoisCities.java - This program prints a message for invalid cities in Illinois.
// Input: Interactive.
// Output: Error message or nothing.

import javax.swing.*;

public class IllinoisCities
{
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 Illinois.
String citiesInIllinois[] = {"Chicago", "Peoria", "Alton", "Springfield", "Batavia", "Naperville", "Kankakee", "Lisle", "Decatur", "Normal"};
boolean foundIt = false; // Flag variable.
int x; // Loop control variable.

// Get user input.
inCity = JOptionPane.showInputDialog("Enter name of city: ");

// 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 Illinois" message should be printed.

System.exit(0);

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Examine all the items in the array
Reference No:- TGS01931725

Now Priced at $20 (50% Discount)

Recommended (93%)

Rated (4.5/5)