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");
	}

}

Recommended Answers

All 3 Replies

Don't use == to compare strings. Use .equals() or .equalsIgnoreCase().

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

instead of
if(words.equals(searchData))

:)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.