• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashtable, constructor and method  RSS feed

 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I implemented this hashtable to simulate a game map...but in creating my methods am getting some errors...seems my hashtable is non recognized ouf of the constructor and my eclipse IDE is signalling me errors like the hashtables dont exists..
now i post a snippet..
to get quicker help...my problems are in the method and i pinpointed them with comments...especially line 143 and 160

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared your HashTables in the Fermi constructor, so they are only in scope inside the constructor. You can't access them from the MoveAlienTo method as they don't exist in that scope, so the compiler rejects the code. You need to declare them as members of the Fermi class.

Other things to note:

1) By convention, method names should start with a lower case letter. The method I mentioned above should be called moveAliensTo()

2) You are comparing Strings using the == operator. This is not the correct way to compare Strings and may give you unexpected results. Use the equals() method instead.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you declared your hashtables inside the Fermi() method (not constructor) so the other methods obviously won't see them.
Also, even if you declared the hashtables as class members your MoveAlienTo() method still wouldn't be able to "see" them, as static methods can't access instance members.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a fair amount of code there.
Thanks for the comments directing to the problem area though

First some general observations/questions:
#1: Your "constructor" has a 'void' return type making it a method.
#2: Coding conventions. Using the standard coding conventions makes it easier for other people to read your code. Variables should be named starting with lower case letters: eg: SecureSectory --> secureSector
#3: Hashtable/Maps:



This sort of datastructure maps one key to one value.
The method "put" takes parameters key and value in that order.
Based on the code you have written, you are putting the same value "H" twice into the map - so one will overwrite the other.

Maybe you meant to use the number as a key and the letter as the value in which case it should be: put(10, "H")?

You can also lose the new Integer part. Autoboxing will do it for you.
Or alternatively Integer.valueOf(10) is preferable to new Integer(10)


#4: Hashtable is an old class. It is preferable to use the Map interface and HashMap class rather than the Hashtable.

And possibly look at renaming "let". I haven't the foggiest idea what that might represent.

#5: Static variables should be avoided.


And after all that your issue:
>my hashtable is non recognized ouf of the constructor
You have declared it it within your constructor. To use it in another method, you should declare it outside of your constructor. Presumably up the top where you have defined pos_Let and pos_Num.


> eclipse IDE is signalling me errors like the hashtables dont exists

Can be fixed by adding

to the top of your class.

In Eclipse you can press CTRL+1 when you have the error highlighted, and it will suggest this fix for you (i.e. Import the java.util.Hashtable class)

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, I missed the return type. It's not a construcor.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mamma mia Mike J Thompson! you are 100 times better than my teacher!
what detailed explanation and straight on point without beating about the bush! Thanks soo much!
thanks to everybody else too!
I'm quite new to java coming from c++ so bare with me!
let meant to mean letter and thats all..I noticed someone complained about that variable name too
but really thanks soo much guys !!!



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some tips to improve your code (I assume you're using a recent version of Java (not a super old version, Java 1.4 or older):

1. Use generics instead of raw types.

2. Program to an interface, not an implementation.

3. Use autoboxing.

4. Class Hashtable is old and superseded by class HashMap. Prefer using HashMap instead of Hashtable.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I corrected some things with you guy's wonderful suggestions and even the eclipse IDE is smiling more with me now
but one lil problem I highlighted earlier that is still not solved...
line 158 : alpha was passed as a string in the method argument..but alpha is also the name of one of my hashtable variables or objects?..
so was hoping to be able to do alpha.containsValue() or alpha.containsKey() from the hashtable class methods..
but something is not right... master Mike Thompson and friends..any nice way to get over this lil problem...I didn't change the hashtable to hashmap yet because for now is working fine if not for that lil part in method movealiento();
thanks soo much for your quality time dedicated and to be dedicated!!!

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still comparing strings with == in this line:

That doesn't work. The == operator compares references, which means that it only checks if the things on both sides of the == refer to the exact same object. With regard to strings, it does not check if the strings have the same content. You could have two String objects with the same content, and then a == b will still be false, because they are two distinct String objects.

If you want to check if a String has a certain content, use equals() instead:
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any HashTables called alpha in the code you posted. Certainly none of the HashTables declared as class fields have that name. You should give them more descriptive names by the way, single-letter names are rarely useful.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jasper..for calling my attention again to that...
but the other problem at line 158 still remains...you got a solution for that?
well..am going to check on hashmaps to see if there are more and better methods now...
in the meanwhile a solution to problem at line 158 may be very much appreciated
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no hashtable called alpha...yes..but alpha is an argument which will be passed into the method movealiento....
so assuming..in the call of the method alpha was passed as "h"..
noticed that i have the hashtable let to verify if h is there at all...
then if h is there...
we go into hashtable h! to do something..this is the logic behind all that am doing...
I hope I made myself clear enough..
thanks
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I presume what you want to do is call:


MoveAlienTo("k", 3)

and then have it map the string "k" to your variable k.

Nope, it ain't gonna do that for you directly.

So you either need to pass the actual map you want to use, or find some way to get a map from its letter identifier.

Here is one way: Declare another map that associates the string name with the object:



 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks soo much Stefan Evans...I implemented my own method..but only at the end of project I realised it was not working...
so I came back here and saw your reply and implemented it and everything is working..and your solution is even more simple...
but just for the sake of learning and understanding ...I'm going to post what I implemented and if any of you can look at it and tell
me why it was not working..I will be very much glad...

may be this would have worked in c++, may be eh :P but I had to delete it and implement the simple straight forward snippet of
Stefan Evans..but I just want to know why what I did here was not working , thanks in advance
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!