-2

I'm a beginner at coding so apologies if this is a silly question/if I missed any tags. I have written a program that uses an array with 10 cities. The output is to ask the user to enter a city, and the program will tell if the array contains it or not. On lines 22 and 23 I get the error: "cannot find symbol". The lines read:

for(int i = 0; i < citiesInMichigan.length; i++){
            if (citiesInMichigan[i].equals(inCity))

If anyone could read over my code and tell me what I'm doing wrong, it would be appreciated. Thanks in advance!

// MichiganCities.cpp - This program prints a message for invalid cities in Michigan. 
// Input: Interactive
// Output: Error message or nothing

import javax.swing.*;

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 Illinois.
        String citiesInIllinois[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"}; 
        boolean foundIt = false;  // Flag variable.
        int x;  // Loop control variable.

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

        // Write your loop here. 
        for(int i = 0; i < citiesInMichigan.length; i++){
            if (citiesInMichigan[i].equals(inCity))
                foundIt = true;
        }
            // Write your test statement here to see if there is 
            // a match.  Set the flag to true if city is found. 


        if (foundIt)
            System.out.println("is in Michigan");
        else System.out.println("Not a city in Michigan");


        // Test to see if city was not found to determine if 
        // "Not a city in Illinois" message should be printed. 

        System.exit(0);
    }
}

Edit: I keep getting this error:

Line 19: Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':1' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
at 
at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:126)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.awt.Toolkit$2.run(Toolkit.java:860)
at java.awt.Toolkit$2.run(Toolkit.java:855)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:854)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:539)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:579)
at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1349)
at javax.swing.UIManager.initialize(UIManager.java:1459)
at javax.swing.UIManager.maybeInitialize(UIManager.java:1426)
at javax.swing.UIManager.getDefaults(UIManager.java:659)
at javax.swing.UIManager.getString(UIManager.java:805)
at javax.swing.UIManager.getString(UIManager.java:822)
at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:474)
at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:440)
at MichiganCities.main(MichiganCities.java:19).

Line 19 reads:

inCity = JOptionPane.showInputDialog("Enter name of city: ");
  • What input are you providing? – Mad Physicist Mar 22 '18 at 01:06
  • 1
    You're using a variable `citiesInMichigan` that has not been declared. This is Java at its most basic and suggests that you should review your notes/book/tutorials. You can find the tutorials here: [The Really Big Index](http://docs.oracle.com/javase/tutorial/reallybigindex.html). You won't regret doing this. – Hovercraft Full Of Eels Mar 22 '18 at 01:07

1 Answers1

-1

You declared "citiesInIllinois", and refer to "citiesInMichigan".

Piotr
  • 504
  • 4
  • 6