AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

newbie thing: problems with next() and whiles

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Gin
Guest





PostPosted: Thu Jul 14, 2005 12:02 am    Post subject: newbie thing: problems with next() and whiles Reply with quote



I have writen the following method and I can't fix the bug.

while (i.hasNext()){

StringBuffer sb = new StringBuffer("");

while (i.next().toString().matches("[^AEIOUaeiou]")) {
String s = i.next().toString();
sb.append(s);
}
while
(i.next().toString().matches("[AEIOUaeiou]")){
String s = i.next().toString();
sb.append(s);
}

sillabaList.add(sb);
}




The error message is
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:426)
at Paraula.procesa(Paraula.java:27)
at Paraula.main(Paraula.java:50)



thanks a lot to everyone!

Back to top
Remi Arntzen
Guest





PostPosted: Thu Jul 14, 2005 12:09 am    Post subject: Re: newbie thing: problems with next() and whiles Reply with quote



a quick fix would be to add "i.hasNext()" to the while loops

i.e.
while (i.hasNext() && i.next().toString()[...]) {
[...]
}

that way "i.next()" will never be called unless there is an element
available.

Back to top
Thomas G. Marshall
Guest





PostPosted: Thu Jul 14, 2005 4:01 am    Post subject: Re: newbie thing: problems with next() and whiles Reply with quote



Remi Arntzen coughed up:
Quote:
a quick fix would be to add "i.hasNext()" to the while loops

i.e.
while (i.hasNext() && i.next().toString()[...]) {
[...]
}

that way "i.next()" will never be called unless there is an element
available.

That's not the whole problem. The problem is that the i.next() is grabbing
the next element, but not saving it. This is from his code:

while (i.next().toString().matches("[^AEIOUaeiou]")) {
String s = i.next().toString();
sb.append(s);
}

This uses i.next() to grab the next value, check it against a regex, then if
it matches, grab /another/ element and append it to the string buffer. He
might need this (untested). NOTE: the loop it /looks/ like he wants will
terminate after the first matching failure---not sure he needs that.:

String str;
while (i.hasNext() && (str = i.next().toString()).matches(......))
{
sb.append(str);
}

or

while (i.hasNext())
{
String str = i.next().toString();
if (str.matches(........))
sb.append(str);
else
break;
}

My suspicion is that he really doesn't want to terminate on the first
failure, but instead search through all of the iteration:

while (i.hasNext())
{
String str = i.next().toString();
if (str.matches(........))
sb.append(str);
}

--
"So I just, uh... I just cut them up like regular chickens?"
"Sure, just cut them up like regular chickens."



Back to top
Gin
Guest





PostPosted: Thu Jul 14, 2005 10:15 am    Post subject: Re: newbie thing: problems with next() and whiles Reply with quote

Thanks a lot!

In the end I have done this and it works fine:


public void procesa(){

StringTokenizer st = new
StringTokenizer(paraula,"ABCDEFGHIJKLMNÑOPQRSTUVWXYZÇabcdefghijklmnñopqrstuvwxyzç1234567890",
true);
while (st.hasMoreTokens()){
lletres.add(st.nextToken());
}
lletres.add(" ");
ListIterator i = lletres.listIterator();
String temp=i.next().toString();
while (i.hasNext()){
StringBuffer sb = new StringBuffer("");


while (i.hasNext() && temp.matches("[^AEIOUaeiou]")) {
sb.append(temp);
temp=i.next().toString();
}

while (i.hasNext() &&
temp.matches("[AEIOUaeiou]")) {
sb.append(temp);
temp=i.next().toString();

}

sillabaList.add(sb);
}

}

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.