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 sting tokenizer in a loop(vv urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "sting tokenizer in a loop(vv urgent)" Watch "sting tokenizer in a loop(vv urgent)" New topic
Author

sting tokenizer in a loop(vv urgent)

rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
how can i use a string tokenizer in a loop.I need to read each line of a file and split it on a delimiter.
My txt file is in the format
A|B|C|D
i have used the following:
try
{
FileReader fr=new FileReader("myfile.txt");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(line,"|");
while (st.hasMoreTokens())
{
a=st.nextToken();
b=st.nextToken();
c=st.nextToken();
d=st.nextToken();
e=st.nextToken();
}
}
}catch(Exception ex){}
What happens is it reads one line then gives nosuchelementfound exception .can u suggest a way of gettin through this.Maybe i need to undefine st object at the end of the loop.If it is tht how do i undefine the object.
sheril she
Greenhorn

Joined: Oct 08, 2002
Posts: 12
instead of assigning the values of the tokens inside the loop use an array and use nextToken() only once within the loop.Use the array values to assign to a,b,...
for eg
int i=0;
StringTokenizer st=new StringTokenizer(line,"|");
while (st.hasMoreTokens())
{
ar[i]=st.nextToken();
i++;
}
rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
tht doesnt help coz i had tried tht too before it keeps givin the exception no matter wht
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Not just urgent, not just very urgent, but more than very very urgent such that you don't have time to write 'very'? Must be important.
First you need to change your display name to comply with our Naming Policy. Accounts with invalid names get deleted, and you have 25 posts. You probably have one more chance before we delete your account.
Now we can get to your question:
Have you tried removing the inner loop that has the while(st.hasMoreTokens()) around it? Looking at what you're trying to do it doesn't look like it's required.
Dave
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Are you sure the StringTokenizer is throwing the exception? You should add some System.out.println() calls to find out how the flow of execution goes. This will give you a better idea of where the exception occurs.
HTH
Layne


Java API Documentation
The Java Tutorial
bob leduc
Greenhorn

Joined: Jan 20, 2003
Posts: 6
add this within your code:
try{
while (st.hasMoreTokens())
{
a=st.nextToken();
b=st.nextToken();
c=st.nextToken();
d=st.nextToken();
e=st.nextToken();
}
} catch (NoSuchElementException e){}
Hope this works?
Originally posted by dimps:
how can i use a string tokenizer in a loop.I need to read each line of a file and split it on a delimiter.
My txt file is in the format
A|B|C|D
i have used the following:
try
{
FileReader fr=new FileReader("myfile.txt");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(line,"|");
while (st.hasMoreTokens())
{
a=st.nextToken();
b=st.nextToken();
c=st.nextToken();
d=st.nextToken();
e=st.nextToken();
}
}
}catch(Exception ex){}
What happens is it reads one line then gives nosuchelementfound exception .can u suggest a way of gettin through this.Maybe i need to undefine st object at the end of the loop.If it is tht how do i undefine the object.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67


catch (NoSuchElementException e){}
Hope this works?

That won't do anything that the original code did not do, except for catching ONLY NoSuchElementExceptions.
The original code is getting the exception since the input string has 4 elements and you are calling the next method 5 times.
The code seems very confused. Why are you calling next multiple times in a loop? That makes no sense. If you know exactly how many elements will be retrived (apparently not) then there's no need for a loop. If you are going to loop, you should call the next method ONCE per loop and exit the loop when there are no more.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

You have 4 tokens on a line, but you're calling nextToken() 5 times in each loop???
You probably want something like this:
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Jamie Robertson:
You have 4 tokens on a line, but you're calling nextToken() 5 times in each loop???
[/CODE]

Correct. I spotted that too, but unfortunately I had to spent too much time doing administration stuff which cut into my helpful time.
I decided the bigger problem was that there was both a loop AND explicit calling of each element.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Erm, what's wrong with String.split()?
- Peter
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Not everyone has Java 1.4 yet. I dont
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sting tokenizer in a loop(vv urgent)