Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need this program to check and array to see if the user input is held within. I did a for loop which I

Hello, I need this program to check and array to see if the user input is held within. I did a for loop which I would think would do what is needed, and i have the return statements in there. but it will not run, what is needed for this to be able to run?

// 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: "+(x+1)); inCity = input.nextLine(); // Write your loop here. for (int x = 0; x < citiesInMichigan.length; x++) { if(incity == citiesInMichigan[x]); { foundIt = true; } } System.out.println("city entered is a city in Michigan"); else System.out.println("city entered is not a city in Michigan"); } System.exit(0);

} // End of main() method. } // End of MichiganCities class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions