If statement problem

I’m trying to loop through an array of iotions before a capital city is outputted. The seems to be an issue with my if statement which throws up an error when I run the file.

Any help would be great.

Thanks


import javax.swing.JOptionPane;
public class Capital {
		public static void main(String[] args){
			//int engine;
			String queury;
			String city[]={"Madrid","Barcelona","Seville", "San Sebastian"};
			
			query = JOptionPane.showInputDialog("What is the capital of Spain");
			
			for(int i=0;i < city.length;i++);
				if  (city = "Madrid")    
	               System.out.println("Yes");
	            else
	                System.out.println("No");
			}
}

‘=’ means: set the variable on the left of this assignment operator to the value of the item on the right.

You’re looking for ‘==’, aka: the ‘equals’ operator.

Scratch that.

You are comparing Strings, so you’ll need to use the String.equals() method.

Hi there

Thanks for the help so far.

Here’s an adjustment to part of the code and it displays Yes when the correct city is entered. However it also displays Yes when the incorrect city is entered. Can anyone help with this.

import javax.swing.JOptionPane;
	   public class Capital {
		public static void main(String[] args){
			String query;
			String city[]={"Madrid","Barcelona","Seville", "San Sebastian"};
			String answer = "Madrid";
			query = JOptionPane.showInputDialog("What is the capital of Spain");
			
			for(int i=0;i < city.length;i++);
			if  (answer == "Madrid")    
	               System.out.println("Yes");
	            else
	                System.out.println("No");
			}
}

any ideas why it’s showing yes for both Madrid and the other cities when it should be printing No for the other cities.

I’d really appreciate some more help as I’m stuck on this.

Thanks

You set answer to Madrid and never update it, then ask if answer is == to Madrid, which it happens to be.

Again, to compare Strings, you must use String.equals().

Because of the way Java makes comparisons using the == operator:

System.out.println( new String( “Madrid” ) == new String( “Madrid” ) );

will print false. This is because the Objects are being compared, not the contents of the Objects.

Conversely:

System.out.println( new String( “Madrid” ).equals( new String( “Madrid”) ) );

will print true.

Thanks again.

I got it working with this adjustment below. However the when the for loop is executed it prints out Yes for Madrid and 3 No’s for the other cities. How do I get it to display only the correct result and not the 4 answers at the same time.

Thanks.

for(int i=0; i< answer.length; i++)
			if  (answer[i] == "Madrid") 
	              System.out.println("Yes");
	           else
	              System.out.println("No");
			}
}

2 Things

A) As I’ve said, now 3 times, using the equivalency operator (==) to compare Strings is wrong and will result in your code not doing what you expect it to do. Due to how String literals are handled, you have been lucky thus far, your luck will not hold.

B) Your code says to print yes if the String is Madrid, otherwise, print no. You’re getting exactly what you’re asking for. Hint: the else clause is optional.

I refer to the the piece of code and it doesn’t appear to be working for some reason even though I think I have everything defined correctly from what I can see:

public class Response {
	public static void main(String[] args){
		
		String responseArray[] = {"Yes","No","Maybe","Unknown"};
		
		System.out.printf("%s%8s\
","Number","Responses");
		
		for(int i = 0; i < responseArray.length; i++)
		{
		System.out.printf("%5d%8d\
",i,responseArray[i]);
		}
	}
}

If anyone knows how to get this to display the Numbers and Responses alongside each other then I’d be grateful as I’m not having any luck in getting these to print out.

Thanks.