File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes hast table and unlimited looping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hast table and unlimited looping" Watch "hast table and unlimited looping" New topic
Author

hast table and unlimited looping

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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:


With Regards,
Anu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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

Joined: Mar 17, 2005
Posts: 146
How to loop through all the elements in Enumeration object and display the values?

PLease help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Moved to Java in General (intermediate)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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 ]

[Jess in Action][AskingGoodQuestions]
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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

Joined: Aug 05, 2005
Posts: 3168
    
  10
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.


Joanne
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thank you Neal.

I shall consider your suggession.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hast table and unlimited looping
 
Similar Threads
need simple example of updating a text file
unable to retrieve ms word files from server socket
exercise getServiceByName
Doubt about StringTokenizer !!
How to send email in text format?