Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hast table and unlimited looping

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

I have a .csv file, in which details about emp-id, emp-name, emp-expenses for Reimbursement and email address are stored.
My application reads this .csv file, and sends a mail to each employee with his id, total expenses for Reimbursement details in text format.

TO calculate each employee's total, first i am storing each employee id in a hashtable, if id again occurs while reading the csv file, the value will be added to the previous data.

For time being,as the first step, i am just inserting values in to hashtable and trying to display it.

My Problem:
The loop is going into unlimited looping. hasMoreElements() is keepon incrementing even if there are only 4 values in enumeration.

code
-----------------------------------------------
try
{String s1="";
File f1 = new File(path);

FileInputStream fstream = new FileInputStream(f1); //new
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
Hashtable ht = new Hashtable();
int count=0;

while((s1=br.readLine())!=null )
{
count++;
out.println("Count= "+count);
StringTokenizer st = new StringTokenizer(s1,",");
if ((st.hasMoreTokens())&&(count>1))
{
String a=st.nextToken().trim();
String b=st.nextToken().trim();;
String c=st.nextToken().trim();;
String d=st.nextToken().trim();;
String e=st.nextToken().trim();;
String f=st.nextToken().trim();;
String g=st.nextToken().trim();;
String h=st.nextToken().trim();;
String i=st.nextToken().trim();;
String j=st.nextToken().trim();;
String k=st.nextToken().trim();;
String l=st.nextToken().trim();;
String m=st.nextToken().trim();;
String n=st.nextToken().trim();;
String o=st.nextToken().trim();;
String p=st.nextToken().trim();;
String q=st.nextToken().trim();;
String mail=st.nextToken().trim();;
String s=st.nextToken().trim();;
out.println("A= "+a+"b="+b+"c="+c+"d="+d+"e="+e+"f="+f+"mail="+mail);
ht.put("a",mail);
String show =(String)ht.get("a");
if (show != null)
{
out.println("HASHTABLE= " +a);
}
} //end of if
}//end of while
int iterate=0;while(em.hasMoreElements())
{
iterate++;
System.out.println("With in hasmore loop= "+iterate);//here, the loop is going into unlimited looping.Eventhough only 4 values of a is available
//out.println("Enum has more elements");
}
}catch(Exception e)
{out.println(e);}
-----------------------------------------------
Any help will be greatly appreciated.

Thanks in advance. :roll:
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing em refers to an Enumeration of keys? In which case, you will need to move the Enumeration on with the nextElement() method. Otherwise your Enumeration will always have more elements.
[ September 13, 2005: Message edited by: Paul Sturrock ]
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul,

The code is working fine now.
i have used
-----------------------
Enumeration em=ht.elements();for(int z=0;z<=10;z++)
{
String data=(String)em.nextElement();
out.println("data="+data);
}
-----------------------

Here, there are 3 values for "a" and when I try to display data, it should display all the values (3 values for "a") and then display,
---------------------------------
e=java.util.NoSuchElementException: Hashtable Enumerator
--------------------------------

But, after executing the for loop for once, it displays the values for the key "a" and then it gives:

e=java.util.NoSuchElementException: Hashtable Enumerator
exception.

WHY it is not displaying all the values for the key "a" ??
(suppose to display 3 values)

Where am i going wroing ?
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to loop through all the elements in Enumeration object and display the values?

PLease help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (intermediate)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ashvini Devi:
How to loop through all the elements in Enumeration object and display the values?

PLease help.


Have you noticed the values() method of Hashtable?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way (there are others!) of iterating over the keys in a Hashtable and printing the keys and values:



A few notes: first, don't use Hashtable in new code: use HashMap instead. Likewise, don't use Enumeration; use Iterator. One nice thing about Iterator is that it has shorter method names, making your code easier to read:



Finally, if you pay a little more attention to formatting in your code, it will make it a lot easier for you to understand what you're doing. Use spaces between parts of an expression, and only put one statement on a line.
[ September 13, 2005: Message edited by: Ernest Friedman-Hill ]
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest Friedman-Hill.

I used Enumeration.nextElement() loop.
The code is working fine now.

Now, i am struck up with another problem.
For a key in hashtable, to insert values, i used hashtable.put(object key,Object value)

Means, i can insert only one value to each key. But, i have 4 to 5 values to insert into one key.
Example,
If, emp-id is the key, i need to insert emp-name, emp-address, emp-phone no. as values to that particular id.

HOw to do that ?

Any help will be appreciated.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ashvini Devi:
Thank you Ernest Friedman-Hill.

I used Enumeration.nextElement() loop.
The code is working fine now.

Now, i am struck up with another problem.
For a key in hashtable, to insert values, i used hashtable.put(object key,Object value)

Means, i can insert only one value to each key. But, i have 4 to 5 values to insert into one key.
Example,
If, emp-id is the key, i need to insert emp-name, emp-address, emp-phone no. as values to that particular id.

HOw to do that ?

Any help will be appreciated.



If all the values are related to each other (as they appear to be) create a class to contain them and then put that into the map.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Neal.

I shall consider your suggession.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic