Java: Hashtables and checking a element is less than value of 40

Associate
Joined
7 Dec 2007
Posts
302
Location
Derbyshire, Tibshelf
For an assignment, one of the tasks is to display someone with a grade less than 40. So far i've got this:
-----------------------------------------------------
Enumeration enumName = NameGrade.keys() ;
Enumeration enumGrade = NameGrade.elements();
txtOutput.setText("");
while ( enumGrade.hasMoreElements() ){
currently converting object to string, then to integer and checking if its less than 40
if ( Integer.parseInt((String)enumGrade.nextElement() ) < 40){
txtOutput.append("Name: " + enumName.nextElement() + " - " + enumGrade.nextElement() + "%\n" );
InputName.setText("");
InputGrade.setText("");
}else{
}
----------------------------------------------------

Just isnt working :/ I input 2 people, one with a grade under and one with over 40 then click the button. At first it was displaying one person but with a different grade ;o then I changed it and it errors out showing nothing at all... I can imagine the error is blindingly obvious if anyone good with Java could check it out :) Hope theres enough information there..

Thanks so much, you would be saving my neck as Im really not getting any of this :P
 
I'd be checking what gets returned by the following, also have you used assertions before? These can be quite handy for debugging.

Code:
Integer.parseInt((String)enumGrade.nextElement())
 
i'd do something more like the following:

Enumeration enumName = NameGrade.keys() ;
Enumeration enumGrade = NameGrade.elements();
txtOutput.setText("");
while ( enumGrade.hasMoreElements() ){
//currently converting object to string, then to integer and checking if its less than 40
Object key = enumName.nextElement();
//get the value that is linked to the key
String grade = (String)NameGrade.get(key);
if ( Integer.parseInt(grade) < 40){
txtOutput.append("Name: " + key + " - " + grade + "%\n" );
InputName.setText("");
InputGrade.setText("");
} else {
...
}



hope that helps
 
thank you so much, it works properly! I get a load of errors though, even though the program still runs and seems to be doing what it needs to do. the first two of the errors are:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: Hashtable Enumerator
at java.util.Hashtable$Enumerator.nextElement(Hashtable.java:1021)

Im fine to leave this lol but.. what is causing that do you think? cause they were my main concern last time, even though it wasnt showing stuff correctly too :D Thanks sooooo much
 
im not entirely sure what those exceptions are for.

im guessing your programming some gui application that doesn't initialise all the variables before using them properly
 
Back
Top Bottom