// IllinoisCities.java - This program prints a message for invalid cities in Ill
ID: 3670021 • Letter: #
Question
// 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);
} // End of main() method.
} // End of IllinoisCities class.
help please! How do I get this program to compile and run correctly?
Explanation / Answer
import javax.swing.*;
public class IllinoisCities
{
public static void main(String args[]) throws Exception
{
String inCity; // name of city to look up in array.
String citiesInIllinois[] = {"Chicago", "Peoria", "Alton", "Springfield", "Batavia", "Naperville", "Kankakee", "Lisle", "Decatur", "Normal"};
boolean foundIt = false;
int x;
inCity = JOptionPane.showInputDialog("Enter name of city: ");
for(int i = 0; i < citiesInIllinois.length; i++){
if (citiesInIllinois[i].equals(inCity))
foundIt = true;
}
if(foundIt)
System.out.println("city is in Illinois");
else System.out.println("city is not in Illinois");
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.