new programmer question about array string searching


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: new programmer question about array string searching

Hybrid View

  1. #1
    skapoor Guest

    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

  2. #2
    Paul Clapham Guest

    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




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center