• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

sting tokenizer in a loop(vv urgent)

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tht doesnt help coz i had tried tht too before it keeps givin the exception no matter wht
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erm, what's wrong with String.split()?
- Peter
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not everyone has Java 1.4 yet. I dont
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic