This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
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

sting tokenizer in a loop(vv urgent)

 
rudolf hitler
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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.
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
tht doesnt help coz i had tried tht too before it keeps givin the exception no matter wht
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
bob leduc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erm, what's wrong with String.split()?
- Peter
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not everyone has Java 1.4 yet. I dont
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic