Results 1 to 3 of 3
- 09-21-2008, 04:32 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 2
- Rep Power
- 0
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.
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.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()
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.
- 09-21-2008, 05:21 PM #2
Member
- Join Date
- Sep 2008
- Location
- Amsterdam
- Posts
- 4
- Rep Power
- 0
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])
- 09-21-2008, 05:32 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM -
Why Equals method should be over ridden in Hashcode?
By skyineyes in forum New To JavaReplies: 1Last Post: 05-26-2008, 04:13 PM -
[SOLVED] If a object equals another object, do they contain the same data?
By bobleny in forum New To JavaReplies: 1Last Post: 04-17-2008, 10:10 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks

Bookmarks