0

I don't know what is wrong with my code here, the if conditions there never run true.

String words [] = new String[10];{ //Store the word string
		words[1] = "apple";
		words[2] = "ice cream";}
String searchData = "";

JTextField mySearchArea = new JTextField(7);

public void init() 
{ 
     setLayout(new FlowLayout(FlowLayout.RIGHT,10,10);
     add(mySearchArea);
     mySearchArea.addActionListener(this); 
}
public void actionPerformed(ActionEvent ae) { 
		mySearchArea.requestFocusInWindow();
		searchData = mySearchArea.getText();
		search();
     }

private void search(){
	for(int i = 1; i <= 2 ; i++ ){          //never run true
		if(words[i] == searchData){
			j=1;
			System.out.println(words[i]);
			break;
		}
		else
			System.out.println("Error");
	}

}
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by sky_B
0

The reason why it never runs true is because you have
if(words == searchData)

instead of
if(words.equals(searchData))

:)

0

Yes, it works now. Can ask another question? For my image(animation) I input from the folder I declared in the project folder, when I draw it in user declare function, it just show 1 seconds then disappear. For image without animation, it can show on my applet, but when drag the window size, it will also disappear.
So, is there anyway to solve it? Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.