SearchName

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    SearchName

    I want to search an array of Students and see if the name i have is the same as the name in one of the entries of the array. This is my code:

    public static Student searchLast(Stud ent st[],String last){
    int N=st.length;
    Student foundStudent;
    st=new Student[N];
    int i=0;
    while(i<N&&!las t.equals(st[i].l_name)){
    i++;
    }
    if(i==N){
    foundStudent=nu ll;
    }
    else{
    foundStudent=st[i];



    }
    return foundStudent;

    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Is there a question?

    I have trouble with this line:
    [CODE=Java]st=new Student[N];[/CODE]
    What do you think its purpose is?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by mia023
      I want to search an array of Students and see if the name i have is the same as the name in one of the entries of the array. This is my code:

      public static Student searchLast(Stud ent st[],String last){
      int N=st.length;
      Student foundStudent;
      st=new Student[N];
      int i=0;
      while(i<N&&!las t.equals(st[i].l_name)){
      i++;
      }
      if(i==N){
      foundStudent=nu ll;
      }
      else{
      foundStudent=st[i];



      }
      return foundStudent;

      }

      You are reassigning the variable "st" as it is passed when the main method called.
      Stop reassigning and start checking directly with "st".

      Debasis Jana

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Suppose I want you to search for a cork screw (how appropriate for the time of the year ;-)
        in five drawers located next to each other. What you'd probaby do is open the left
        drawer and search for the item, if you haven't found it you open the next drawer.
        Suppose the cork screw is in the second drawer; do you continue your search in
        the third, fourth and fifth drawer? I don't think so.

        kind regards,

        Jos

        Comment

        Working...