![]() |
I'm having a little problem with a piece of homework I have to do... Its unlikely that I will get any help by tomorrow, but I'm posting this just on the offchance.
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> public void findIt(String searchValue) { //Variable declaration char currentChar; int asciiValue = 0, hashTotal = 0, index = 0; boolean found = false; if (searchValue.length() != 3) System.out.println("Error: the code must be three + characters long!"); else { //work out the hash total for each character in the code for (int position = 0; position < searchValue.length(); position++) { //currentChar is set to the character in the index position //being worked upon currentChar = searchValue.charAt(position); //the ASCII value of the character is found asciiValue = (int)currentChar; //the ASCII value is added to the hashTotal hashTotal+= asciiValue; } //the hash code is worked out for the hashTotal hashCode = hashTotal%hash.length; if (hash[hashCode] == "free") { System.out.println("The requested code was not found."); } ********** else if (hash[hashCode] == searchValue) { System.out.println("The requested code was found in index position " + hashCode + "."); } ************ else { for (int indexPosition = 0; indexPosition < overflow.length; indexPosition++) { if (overflow[indexPosition] == searchValue) { found = true; index = indexPosition; System.out.println("found"); break; } } if (found) { System.out.println("The requested code was found in index position " + (index + 51) + "."); } else System.out.println("The requested code was not found."); } //reset hash total hashTotal = 0; } }</pre>[/QUOTE]Its part of a program to hash an array, and this is part of the method to search for a value entered in another class. (A three letter string). I have established that the program is working out the correct hash codes for the searched for variable. The problem seems to lie in the part between the asterisks. Everything else works fine. It just doesn't seem to realise that the string in hash[hashCode] is the same as searchedFor, but they are... without a shadow of doubt as far as I can see... Have I just typed in the else if statement wrong or something? I'm going crazy here trying to figure it out [img]tongue.gif[/img] |
Try something like this, I haven't done Java in a long time, so the syntax might not be 100% correct:
if(new String(hash[hashCode]).equals(searchValue)) {...} the point is IIRC you can't compare Strings with the == operator, you need to use the .equals method. And just in case the returned value from has is not in the right format, I'm constructing a new String with it. You can do it in 2 steps as well, to make sure, i.e. String s = new String(hash...) then if s.equals(searchvalue) etc. Let me know if that helps... [ 02-17-2005, 08:17 AM: Message edited by: Vaskez ] |
dammit!
Thanks... only a few months into the course and forgetting all I've learned [img]tongue.gif[/img] |
so did it work? I wanna know if I remembered correctly! :D
|
Yes... Except I used equalsIgnoreCase to be clever :D
|
All times are GMT -4. The time now is 11:27 AM. |
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved