Results 1 to 3 of 3
  1. #1
    Dave0703 is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default Creating a new equals() method help

    Hello, I'm having trouble with the comparison of an array of football teams and an array of team names that may or may not be in the football team array. I have read both .txt files into their own arrays and parsed the team array with substring in order to isolate the team name. I know I can read both the search array and the team names individually because I tested it with a println. I think my problem is in the comparison "if" statement.
    Java Code:
    public void seqSearch()//Sequential(linear) search
        {
        int j=0, s;
        //int m = j+1;
            for(s=0; s<searchSize; s++)//increments through the search array
            {    
              for(j=0; j<teamArray.length; j++)//increments through the team array
               if(teamArray[j].getName().equals(search[s]))//I think this is where the problem is
               {               
                   System.out.println("Found " + teamArray[j].getName() + "after" + j + "searches.");
                   break;                  
               }
               else
               {
                   System.out.println(teamArray[j].getName());//prints through all of the names during each search for testing 
                   if(j == teamArray.length-1)
                   {
                       System.out.println("Can't find " + search[s] + " after " + j + " searches");
                   }                            
                }
            }//end for
        }//end seqSearch()
    My output prints the names of each team on their own line and at the end it always says that it can't find each search term.
    Ex.
    Bears
    Bengals
    Bills
    ...
    Vikings
    Can't find Bills after 32 searches
    Bears
    Bengals
    Bills
    ...
    Vikings
    Can't find Eagles after 32 searches
    etc

    I'm pretty sure I have to create a new equals method but I don't understand how else I'm supposed to compare them other than as I have in the code above. teamArray[j].getName returns a string teamName and search[] is an array of type String so shouldn't each search[s] also be a string? If that is the case(both are strings) how else does one compare two strings besides the built in equals() method?

    Any help would be much appreciated.

  2. #2
    oliemansm is offline Member
    Join Date
    Sep 2008
    Location
    Amsterdam
    Posts
    4
    Rep Power
    0

    Default

    By looking at your println statements it seems that teamArray[j].getName() includes a space at the end, while search[s] does not. Try: teamArray[j].getName().trim().equals(search[s])

  3. #3
    Dave0703 is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-19-2008, 11:58 PM
  3. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 04:13 PM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •