Ironworks Gaming Forum

Ironworks Gaming Forum (http://www.ironworksforum.com/forum/index.php)
-   General Discussion (http://www.ironworksforum.com/forum/forumdisplay.php?f=36)
-   -   Java help (http://www.ironworksforum.com/forum/showthread.php?t=92987)

Callum 02-17-2005 07:47 AM

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(&quot;Error: the code must be three + characters long!&quot;);
else
{
//work out the hash total for each character in the code
for (int position = 0; position &lt; 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] == &quot;free&quot;)
{
System.out.println(&quot;The requested code was not found.&quot;);
}
********** else if (hash[hashCode] == searchValue)
{
System.out.println(&quot;The requested code was found in index position &quot;
+ hashCode + &quot;.&quot;);
}
************
else
{
for (int indexPosition = 0; indexPosition &lt; overflow.length; indexPosition++)
{
if (overflow[indexPosition] == searchValue)
{
found = true;
index = indexPosition;
System.out.println(&quot;found&quot;);
break;
}
}

if (found)
{
System.out.println(&quot;The requested code was found in index position &quot;
+ (index + 51) + &quot;.&quot;);
}
else
System.out.println(&quot;The requested code was not found.&quot;);
}
//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]

Vaskez 02-17-2005 08:15 AM

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 ]

Callum 02-17-2005 08:33 AM

dammit!

Thanks... only a few months into the course and forgetting all I've learned [img]tongue.gif[/img]

Vaskez 02-17-2005 08:41 AM

so did it work? I wanna know if I remembered correctly! :D

Callum 02-17-2005 09:46 AM

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