new programmer question about array string searching
Following is a code I am trying to make it work for learning purposes. tf1,tf2,tf5
are Textfield, rs database, data is array object
I am trying to search for a string in textfield tf5 when ever I press search
button. This code does find
the string I am looking for but it continues to search and always displays
the last item on the array. Whats wrong with this
code I can't seem to solve the problem. any help would be appreciated.
String m=" ";
m=tf5.getText(); //get the string from textfield
for (int i=0; i<rs.n; i++){
if (rs.data[i].lLast.equals(m)) //here it looks as it found the text
and then continues as if did not find anything
tf1.setText(rs.data[i].lLast);
tf2.setText(rs.data[i].fFirst);
/tf5.setText("");
}
}
class database {
Record rs=new Record();
public Record[] data = new Record[1000];
public int n=0;
public void store (Record rs) { //store String lLast,fFirst,sSS,aAge
data[n] =rs;
n++;
}
}
THanks
sudhir kapoor
Re: new programmer question about array string searching
Is there a "{" after "if (rs.data[i].lLast.equals(m))" in your actual code?
There should be, otherwise your {} do not match up and the code will not
compile.
Assuming there is, however, you are correct that your code continues to
search after it finds an entry that satisfies .equals(m). This is because
you do not tell it to stop searching then. If you want it to stop searching
when it finds the first "equal" entry, then you need to have code like
for (int i=0; i<rs.n; i++) {
if (...equal test...) {
...we found something, deal with it...;
break;
}
}
The "break" statement causes control to exit from the for-loop.
skapoor <sudhir.kapoor@mailcity.com> wrote in message
news:399c4922$1@news.devx.com...
>
> Following is a code I am trying to make it work for learning purposes.
tf1,tf2,tf5
> are Textfield, rs database, data is array object
> I am trying to search for a string in textfield tf5 when ever I press
search
> button. This code does find
> the string I am looking for but it continues to search and always
displays
> the last item on the array. Whats wrong with this
> code I can't seem to solve the problem. any help would be appreciated.
>
>
> String m=" ";
> m=tf5.getText(); file://get the string from textfield
>
> for (int i=0; i<rs.n; i++){
>
> if (rs.data[i].lLast.equals(m)) file://here it looks as it found the
text
> and then continues as if did not find anything
>
>
> tf1.setText(rs.data[i].lLast);
> tf2.setText(rs.data[i].fFirst);
> /tf5.setText("");
>
> }
> }
>
>
> class database {
> Record rs=new Record();
> public Record[] data = new Record[1000];
> public int n=0;
> public void store (Record rs) { file://store String
lLast,fFirst,sSS,aAge
> data[n] =rs;
> n++;
>
> }
> }
>
> THanks
> sudhir kapoor