Java Forum / General / January 2006
Hashtable
tuurbo46@yahoo.co.uk - 24 Jan 2006 17:57 GMT Hi
Im currently im in a bit of a corner. I am able to put data in a hashtable, but now im in a muddle getting the data out.
How do i change the below method to retrive the data without using the string?
// declared at top of source file private TextField userName = new TextField("Name:.","",20,TextField.ANY); private TextField userNumber = new TextField("Number:.","",20,TextField.ANY);
//Declaration of hashtable Hashtable hT = new Hashtable(5);
//method in code public void View() { Enumeration e1=hT.keys(); while(e1.hasMoreElements()) { userName=(String)e1.nextElement(); userNumber=(String)hT.get(userName); display.setCurrent(userNumber); } }
Oliver Wong - 24 Jan 2006 18:12 GMT > Hi > [quoted text clipped - 24 lines] > } > } If you don't care about the keys, use the values() method instead of the keys() method to get an enumeration of values.
Are you using Java 1.5 or 1.4 or some other version?
- Oliver
zero - 24 Jan 2006 18:15 GMT tuurbo46@yahoo.co.uk wrote in news:1138125463.066937.5490 @f14g2000cwb.googlegroups.com:
> Hi > [quoted text clipped - 24 lines] > } > } userName is a TextField, so why are you casting the retrieved element to String? What's TextField anyway? java.awt.TextField has no constructor that matches what you're using.
Using Hashtable is easy, you just have to make sure that what you put in matches what you're trying to get out. You can't put in an Acorn and expect to get an Oak back.
Hashtable table = new Hashtable(); table.put("1", new Integer(1)); table.put("2", new Integer(2));
for (Enumeration e = table.keys(); e.hasMoreElements(); ) { String key = e.nextElement(); Integer value = (Integer)table.get(key); System.out.println(key " => " value); }
tuurbo46@yahoo.co.uk - 24 Jan 2006 18:25 GMT Hi
Im currently using jBuilder 9 and J2me within this enviroment. The (System.out....) value is not used. Would i use the below code now? Also the J2me enviroment does not like ( " => "), does anybody have any ideas
for (Enumeration e = table.keys(); e.hasMoreElements(); ) { String key = e.nextElement(); Integer value = (Integer)table.get(key); display.setCurrent(key " => " value); }
Oliver Wong - 24 Jan 2006 18:56 GMT > Hi > [quoted text clipped - 9 lines] > display.setCurrent(key " => " value); > } You should probably specify that you're using J2ME. If you don't say anything, people might think you're using J2SE, and the libraries are different between J2ME and J2SE.
The problem with " => " is that you need to add the + operator to do string concatenation. Try something like this:
<code> for (Enumeration e = table.keys(); e.hasMoreElements(); ) { String key = e.nextElement(); Integer value = (Integer)table.get(key); display.setCurrent(key + " => " + value); } </code>
- Oliver
zero - 24 Jan 2006 19:12 GMT "Oliver Wong" <owong@castortech.com> wrote in news:YLuBf.109771$AP5.28395 @edtnps84:
> The problem with " => " is that you need to add the + operator to do > string concatenation. duh on me
Roedy Green - 24 Jan 2006 19:48 GMT > display.setCurrent(key " => " value); You a NetRexx coder? I wonder about the possibility of someday allowing either space or some other than + character as the concatenation operator, perhaps _ or # or maybe even :
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Oliver Wong - 24 Jan 2006 20:08 GMT >> display.setCurrent(key " => " value); > > You a NetRexx coder? > I wonder about the possibility of someday allowing either space or > some other than + character as the concatenation operator, perhaps _ > or # or maybe even : A few languages use the period character. In a language I'm developing, it's the ~ character. E.g. "Hello"~" world!"; my rational is that it's about as difficult to type as the + character on a QWERTY keyboard (shift, and then one of the two edges of the top most row), and the shape of the character is vaguely reminiscent of using a string to tie two things together.
- Oliver
Stefan Ram - 24 Jan 2006 20:42 GMT >A few languages use the period character. In a language I'm >developing, it's the ~ character. E.g. "Hello"~" world!"; my >rational is that it's about I am using in also in a language, I'm developing, namely in Unotal, for the same purpose. [1]
I took this solution from Perl 6, where the tilde »~« also is used for string concatenation.
[1] Section "5.1" (with some typos) in http://www.purl.org/stefan_ram/html/unotal.html#anchor14
Roedy Green - 24 Jan 2006 22:37 GMT > A few languages use the period character. In a language I'm developing, >it's the ~ character. E.g. "Hello"~" world!"; my rational is that it's about >as difficult to type as the + character on a QWERTY keyboard (shift, and >then one of the two edges of the top most row), and the shape of the >character is vaguely reminiscent of using a string to tie two things >together. ~ is already taken in Java. Pl/I's || is also taken.
~ would be a nice choice. It would not be confused with any other operator if it weren't already taken and I agree it has the look of a tie operator.
Perhaps when Unicode takes a foothold we might use \u2903 \u2908 \u2af6 \u2a6a \u2a1d \u25be for a concatenation operator.
see http://mindprod.com/jgloss/unicode.html to view the glyphs.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 24 Jan 2006 23:03 GMT > ~ is already taken in Java. Pl/I's || is also taken. Not necessarily a problem. + and - are used as both unary and binary operators.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Roedy Green - 25 Jan 2006 00:59 GMT On Tue, 24 Jan 2006 23:14:16 +0000, Thomas Hawtin <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone who said :
>Not necessarily a problem. + and - are used as both unary and binary >operators. Even you could, my whole reason to avoid + is so that you don't use the same symbol for two unrelated purposes even if the compiler can figure it out.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 25 Jan 2006 16:25 GMT > On Tue, 24 Jan 2006 23:14:16 +0000, Thomas Hawtin > <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone [quoted text clipped - 6 lines] > the same symbol for two unrelated purposes even if the compiler can > figure it out. It is traditional in C-style languages.
& - reference and and * - dereference and multiply + - addition and string concatenation (a non-commutative operation) . - instance qualifier and decimal point ) - bracketing, indicating a function and end of if/while/for expression. > - greater than and end of generic type list >> - read, shift right and end of two generic type lists >>> - logical shift right and end of three generic type lists
Also % and ^ in Microsoft extensions to C++.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Oliver Wong - 25 Jan 2006 16:35 GMT >> On Tue, 24 Jan 2006 23:14:16 +0000, Thomas Hawtin >> <usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone [quoted text clipped - 20 lines] > > Also % and ^ in Microsoft extensions to C++. It is occasionally pointed out that although Java claims operator overloading is bad (which is one of the reason Java doesn't allow operator overloading), it's somewhat hypocritical that Java overloads the + operator to mean both numerical addition and string concatenation.
So coming up with a new operator for string concatenation might be seen as a way to fix this inconsistency.
- Oliver
Roedy Green - 25 Jan 2006 20:03 GMT > So coming up with a new operator for string concatenation might be seen >as a way to fix this inconsistency I'm fine with leave + the same in the canonical text files. I just want to have an IDE display it differently, even if just a different colour + for addition and concatenation to make errors stand out. Java gets "confused" if you don't use enough ().
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Stefan Ram - 25 Jan 2006 23:00 GMT >I'm fine with leave + the same in the canonical text files. I just >want to have an IDE display it differently, even if just a different >colour + for addition and concatenation to make errors stand out. Java >gets "confused" if you don't use enough (). I a sense, there is another overload.
Some programmers (ab)use the "+" operator to convert to java.lang.String, as in:
"" + 2
instead of
java.lang.String.valueOf( 2 )
So "+" could mean:
- numerical addition, - string concatenation, or - conversion to string.
However, I have no problem with all this. Human natural languages tend to overload nearly every word with different meanings, and polymorphism in OOP also might be considered as some kind of "overloading" of a name with many ("poly") implementations ("morphs"). So if it is used in natural languages and in OOP, why should it be a problem when used for some operators?
Stefan Ram - 25 Jan 2006 23:06 GMT >"" + 2 There is also similar other bad way, to convert to String, namely,
"".valueOf( 2 )
zero - 25 Jan 2006 23:57 GMT ram@zedat.fu-berlin.de (Stefan Ram) wrote in news:conversion-20060126000436 @ram.dialup.fu-berlin.de:
> There is also similar other bad way, to convert to String, namely, > > "".valueOf( 2 ) This is a side-effect of static factory methods. Perhaps it could be overcome if language designers only allowed static methods to be called with the ClassName.methodName() syntax, and not objectName.methodName().
Btw, I don't see anyone who knows a little about Java using the syntax you mentioned, even if they are unaware of the performance hit. String.valueOf() is more natural than "".valueOf().
Andrew McDonagh - 26 Jan 2006 00:32 GMT > ram@zedat.fu-berlin.de (Stefan Ram) wrote in news:conversion-20060126000436 > @ram.dialup.fu-berlin.de: [quoted text clipped - 10 lines] > mentioned, even if they are unaware of the performance hit. > String.valueOf() is more natural than "".valueOf(). I had a terrible time trying to stop some of our devs doing "" + 2 or "".valueof(2) most don't see the problem
and only see that its less keyboard typing...
like the thread on variable naming, where someone replied that I must lilke keyboarding typing because I'd rather use a meaningful english sentence, rather than negate the result of another method..
as in...
if( noMoreElements()
instead of..
if (! hasMoreElements())...
Typing away atthe keyboard is irrelvant... code and design clarity are much more important.
Andrew
Roedy Green - 26 Jan 2006 01:19 GMT > So if it is used in natural languages and in OOP, why > should it be a problem when used for some operators? I gave a reading comprehension test on this + ambiguity matter to room full of high powered Java coders at a software conference. Most failed.
See http://mindprod.com/jgloss/gotchas.html#CONCATENATION
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Oliver Wong - 26 Jan 2006 16:44 GMT >>I'm fine with leave + the same in the canonical text files. I just >>want to have an IDE display it differently, even if just a different [quoted text clipped - 17 lines] > - string concatenation, or > - conversion to string. At the bytecode level, + is very heavily overloaded. It could be integer addition, or long addition, or double precision floating point addition, or single precision floating poitn addition. At the "Java" level, we usually gloss over that, and just consider all those operations to be the same (even though the algorithm could vary wildly, for example between integral addition and floating point addition).
The "string concatenation" and "conversion to string" isn't two different overloaded operations. Actually, when you do the string concatenation, string conversion always occurs. So for example, the express "Hello " + "world" converts its arguments to strings (which is trivial since they are both already strings), and then does the concatenation.
> However, I have no problem with all this. Human natural > languages tend to overload nearly every word with different [quoted text clipped - 4 lines] > So if it is used in natural languages and in OOP, why > should it be a problem when used for some operators? 1 + 1 + " foo" == "2 foo" "foo " + 1 + 1 == "foo 11"
If you follow the rule of least astonishment, the above behaviour shouldn't happen, because most people would be surprised by it. Why are they surprised? Because they are confusing when + is performing numerical addition and when it is performing string concatenation.
I'm not saying ALL operator overloading is bad; I'm just saying that overloading an operator to perform both numerical addition and string concatenation can lead to surprising behaviour.
- Oliver
Stefan Ram - 26 Jan 2006 17:22 GMT >1 + 1 + " foo" == "2 foo" >"foo " + 1 + 1 == "foo 11" > If you follow the rule of least astonishment, the above behaviour >shouldn't happen, because most people would be surprised by it. Reminds me of the following example, which also involves "+":
public class Main { public static void main( final java.lang.String[] args ) { java.lang.System.out.println( 1E20 + 1 - 1E20 ); java.lang.System.out.println( 1E20 - 1E20 + 1 ); }}
Roedy Green - 24 Jan 2006 19:45 GMT >Im currently im in a bit of a corner. I am able to put data in a >hashtable, but now im in a muddle getting the data out. see http://mindprod.com/jgloss/hashtable.html and http://mindprod.comj/jgloss/hashmap.html which is the modern replacement.
for sample code.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
tuurbo46@yahoo.co.uk - 24 Jan 2006 22:06 GMT Hi
Thanks for you help guys. The dam thing still does not work. I think my teacher should help more, but he comes out with the c...p line of, degrees are read and not taught.
Anyway, does anybody know of a pay site where i can get some programming problems solved (writen for me)?
Thanks again for help.
Oliver Wong - 24 Jan 2006 22:23 GMT > Hi > [quoted text clipped - 4 lines] > Anyway, does anybody know of a pay site where i can get some > programming problems solved (writen for me)? Even if you go to the pay site, you will have to address these concerns (which I feel you haven't done adequately yet):
What does your code look like? What is your code supposed to do? What's the difference between what it's doing now and what it's supposed to do?
- Oliver
tuurbo46@yahoo.co.uk - 24 Jan 2006 22:41 GMT Hi
Currently im doing some uni homework and we have to modify a mobile phone application. This involves starting with a blank phone, and adding the following items:
add entry delete entry view search
So far i am able to get the main screen going (as above). With this working i can select the add entry - this works fine. At this point i add name and telephone number and this gets saved to a hashtable. At this point i struggle because i cannot get data from hashtable to view names and telephone numbers. Below is the list of my code snippets
//Declared at top of source code private TextField userName = new TextField("Name:.","",20,TextField.ANY); private TextField userNumber = new TextField("Number:.","",20,TextField.ANY);
//Hashtable Hashtable hT = new Hashtable(10);
// adding people to hashtable
else if(event.equals("Save"))
{
hT.put(userName.getString(),userNumber.getString());
MainMenu();
}
// viewing people in hashtable - this does not work
public void View()
{
Enumeration e1=hT.keys();
while(e1.hasMoreElements())
{
userName = (String)e1.nextElement();
userNumber=(String)hT.get(userName);
display.setCurrent(userNumber);
}
//Fields for entering name and number
public void textBox()
{
display = Display.getDisplay(this);
Form f = new Form("Add:");
f.append(userName);
f.append(userNumber);
f.addCommand(backCommand);
f.addCommand(SaveCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
}
Thanks guys.
Oliver Wong - 24 Jan 2006 23:50 GMT > Hi > [quoted text clipped - 81 lines] > > Thanks guys. Okay, now I have a much better understanding of what you are trying to do. However, what do you mean by "this does not work"? What did you expect to happen, and what is the code doing instead?
For example, "I expected for the user name to be displayed on the screen, but instead I am getting a NullPointerException".
BTW, I notice there seems to be a closing brace missing for the method view().
- Oliver
tuurbo46@yahoo.co.uk - 25 Jan 2006 09:35 GMT Hi
The missing bracket was a copy past error. The null pointer problem has not happend on my enviroment. I cannot get it past the compiler. The (string) in the view method shows red.
How would you now recommend i change the view method?
Damian Brunold - 25 Jan 2006 13:32 GMT > The (string) in the view method shows red. If TextField is some kind of widget, then you most probably need to do something like
while(e1.hasMoreElements()) { userName.setText((String)e1.nextElement()); userNumber.setText((String)hT.get(userName));
instead of
while(e1.hasMoreElements()) { userName=(String)e1.nextElement(); userNumber=(String)hT.get(userName);
Damian
Oliver Wong - 25 Jan 2006 16:00 GMT >> The (string) in the view method shows red. > [quoted text clipped - 12 lines] > userName=(String)e1.nextElement(); > userNumber=(String)hT.get(userName); Damian's suggestion here looks promising. But if it doesn't solve your problem, perhaps you could give the exact error message that the compiler is giving you?
- Oliver
tuurbo46@yahoo.co.uk - 25 Jan 2006 18:33 GMT Hi
Thanks for your advice.
Im not next to the enviroment right now. I will get back to you tomorrow.
Thanks again.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|