| View previous topic :: View next topic |
| Author |
Message |
Gin Guest
|
Posted: Thu Jul 14, 2005 12:02 am Post subject: newbie thing: problems with next() and whiles |
|
|
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
|
Posted: Thu Jul 14, 2005 12:09 am Post subject: Re: newbie thing: problems with next() and whiles |
|
|
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
|
Posted: Thu Jul 14, 2005 4:01 am Post subject: Re: newbie thing: problems with next() and whiles |
|
|
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
|
Posted: Thu Jul 14, 2005 10:15 am Post subject: Re: newbie thing: problems with next() and whiles |
|
|
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 |
|
 |
|