Visit the Ironworks Gaming Website Email the Webmaster Graphics Library Rules and Regulations Help Support Ironworks Forum with a Donation to Keep us Online - We rely totally on Donations from members Donation goal Meter

Ironworks Gaming Radio

Ironworks Gaming Forum

Go Back   Ironworks Gaming Forum > Ironworks Gaming Forums > General Discussion
FAQ Calendar Arcade Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 02-17-2005, 07:47 AM   #1
Callum
Symbol of Cyric
 

Join Date: October 21, 2004
Location: Vancouver, BC
Age: 35
Posts: 1,143
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.

code:
	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;
}
}
[/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]
__________________
[img]\"http://img11.imageshack.us/img11/4763/callumavataranimated4ff.gif\" alt=\" - \" />
Callum is offline   Reply With Quote
Old 02-17-2005, 08:15 AM   #2
Vaskez
Takhisis Follower
 

Join Date: April 30, 2001
Location: szép Magyarország (well not right now)
Posts: 5,089
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 ]
__________________
Too set in his ways to ever relate
If he could set that aside, there'd be heaven to pay
But weathered and aged, time swept him to grave
Love conquers all? Damn, I'd say that area's gray
Vaskez is offline   Reply With Quote
Old 02-17-2005, 08:33 AM   #3
Callum
Symbol of Cyric
 

Join Date: October 21, 2004
Location: Vancouver, BC
Age: 35
Posts: 1,143
dammit!

Thanks... only a few months into the course and forgetting all I've learned [img]tongue.gif[/img]
__________________
[img]\"http://img11.imageshack.us/img11/4763/callumavataranimated4ff.gif\" alt=\" - \" />
Callum is offline   Reply With Quote
Old 02-17-2005, 08:41 AM   #4
Vaskez
Takhisis Follower
 

Join Date: April 30, 2001
Location: szép Magyarország (well not right now)
Posts: 5,089
so did it work? I wanna know if I remembered correctly!
__________________
Too set in his ways to ever relate
If he could set that aside, there'd be heaven to pay
But weathered and aged, time swept him to grave
Love conquers all? Damn, I'd say that area's gray
Vaskez is offline   Reply With Quote
Old 02-17-2005, 09:46 AM   #5
Callum
Symbol of Cyric
 

Join Date: October 21, 2004
Location: Vancouver, BC
Age: 35
Posts: 1,143
Yes... Except I used equalsIgnoreCase to be clever
__________________
[img]\"http://img11.imageshack.us/img11/4763/callumavataranimated4ff.gif\" alt=\" - \" />
Callum is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
JAVA Help Needed Timber Loftis General Discussion 4 04-29-2005 04:02 PM
More Java Help Callum General Discussion 4 03-14-2005 08:19 PM
Window XP and JAVA help!! AngelofDeath General Conversation Archives (11/2000 - 01/2005) 2 10-31-2003 11:50 AM
Could someone help me with a Java/IE prob? Melusine General Conversation Archives (11/2000 - 01/2005) 8 04-13-2003 03:24 AM
Java help needed Memnoch General Conversation Archives (11/2000 - 01/2005) 5 05-27-2002 06:16 AM


All times are GMT -4. The time now is 08:21 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved