String comparision

I have 2 string arrays

String[] s1 ={"aroma" ,"Huge","dock" ,"divide"};

String[] s2 ={"a pleasant smell","big","place where ships are loaded","to split up"};

The word in the textfield searching from first string array and if found writing the corresponding element of second array in other textfield

publicvoid actionPerformed(ActionEvent ae)

{

JTextField source = (JTextField)ae.getSource();

// check if event occurred on pro component

if (text1.getText() !=""){

if (source == text1)

for (i=0; i<4;i++){

if (s1[i].equals(text1.getText()))

text2.setText(s2[i]);

else

text2.setText("word not known");

}

}

but codes are unable to search the text

can anybody help?

[1535 byte] By [605346seemapa] at [2007-11-26 12:18:11]
# 1
What do you mean codes are unable to search, you are using equals?Also don't use two string arrays, Use a map instead!
642814zadoka at 2007-7-7 14:57:24 > top of Java-index,Archived Forums,Socket Programming...
# 2

ext1.getText() != "") {

Don't use == and != when comparing Object values, use the equals() method defined by Object.

for (i=0; i<4;i++){

Hardcoding magic numbers is no good, replace "4" by "s1.length".

text2.setText("word not known");

This else block will be entered for each array element besides the one matching. You should break from the loop if you find the element.

172863quittea at 2007-7-7 14:57:24 > top of Java-index,Archived Forums,Socket Programming...
# 3

Hallo,

here's slightly modified function with few tips in comments. Searching array is fine, but bit inefficient comparing to Map from collection classes.

Please don't forget that java strings are case sensitive, thus you have to write string exactly the same way as in array (e.g. huge is not in your dicitionary)

public void actionPerformed(ActionEvent ae)

{

// Skip when not text1 action

Object source = ae.getSource();

if (source != text1)// comparation works for object well dont worry

return ;

String inputText = text1.getText();

// Skip when empty input

// When possible do not dive your curly brackets { too deep :-)

// if () { if () { if () { if () { is hard to read and maintain

if (inputText.length() == 0)

return ;

// search array manualy from the back (thats bit faster),

// you can also use Arrays.binarySearch or Map from collection

// classes

for (int i = s1.length - 1; i >= 0; i--) {

// use s1.length not hardcoded value

if (s1[i].equals(inputText)) {

text2.setText(s2[i]);

return ;// done we found and set it

}

}

// not found, do not setText it in loop -- each setText generate

// many events that have to be processed

text2.setText("word not known");

}

Sample using Map instead of two arrays

public static Map<String, String> s = new HashMap<String,String>();

static {

s.put("Java","nice programing language");

// ...

}

public void actionPerformed(ActionEvent ae)

{

// Skip when not text1 action

Object source = ae.getSource();

if (source != text1)

return ;

// Lookup and set translated text

String translated = s.get(text1.getText());

text2.setText(translated);

}

579347a3cchana at 2007-7-7 14:57:24 > top of Java-index,Archived Forums,Socket Programming...
# 4

Thx a3cchan for solving my problem u suggested me that we can do it by Arrays.binarySearch and I tried

[code] [public void actionPerformed(ActionEvent ae)

{

// Skip when not text1 action

Object source = ae.getSource();

if (source != text1)

return ;

String inputText = text1.getText();

int index Arrays.binarySearch(s1,inputtext);

text2.setText(s2[index]);

}

/code]

And this also when i pressed enter nothing came in text2. Is anything wrong?

secondly; when i tried to put [} in if loop as was asked by teacher to do that way and that also didn't work.

can u advise ?

605346seemapa at 2007-7-7 14:57:24 > top of Java-index,Archived Forums,Socket Programming...