I'm very new to this, this being only my second project for my class. When I run this, no matter what my input is, x is always > 0. I ran the debugger and found that it increments x at both city 1 and 2, so I figured I do not have my if statements set up correctly. We were instructed to prompt the user for 3 cities, and if any are a match, we tell them. Thanks in advance!
import java.util.Scanner; public class PR2 {
public static void main(String[] args)
{
int x = 0;
Scanner input = new Scanner(System.in);
String[] City = new String[3];
City[0] = "Orlando";
City[1] = "New York";
for (int i = 0; i < 3; i++)
{
System.out.println("Please enter a city you have visited.");
City[i] = input.nextLine();
}
for (int i = 0; i < 3; i++)
{
if (City[i] == City[0])
{
x++;
}
else if (City[i] == City[1])
{
x++;
}
}
if (x > 0)
{
System.out.println("At least one city you have entered is a match.");
}
if (x == 0)
{
System.out.println("Sorry, none of the cities you have entered are a match.");
}
}
}