Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > hasNext?

Reply
Thread Tools

hasNext?

 
 
KyoGaSuki
Guest
Posts: n/a
 
      02-28-2008
What is hasNext? I know you use it when you are reading from a file
and it will stop when it has nothing more to read, but how would you
use it? I have seen a couple of different examples of it, but all
they do is make me more confused. I just can't seem to understand
it...can anyone help?
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      02-28-2008
KyoGaSuki <(E-Mail Removed)> wrote:
> What is hasNext? I know you use it when you are reading from a file
> and it will stop when it has nothing more to read, but how would you
> use it? I have seen a couple of different examples of it, but all
> they do is make me more confused. I just can't seem to understand
> it...can anyone help?


e.g.:

Iterator x= ...;
while (x.hasNext()) {
... x.next() ...
}

You must make sure, that you only call ".next()" if the
previous ".hasNext()" returned true. You also shouldn't
call .next() more than once in the loop, except if you
also check again for .hasNext() .

 
Reply With Quote
 
 
 
 
sihle87@gmail.com
Guest
Posts: n/a
 
      02-28-2008
i think what you want is "hasNext()".
forgive me if i'm mistaken but method "hasNext()" is a boolean method
which returns either true or false, depending on the expression @
hand.
for an instance, suppose you have a string as follows:

String str="my problems are gone!";

//(say) you want to print tokens(just words of the string) you'll do
the following.

while(str.hasNext())//here you are actually asking whether is it still
true that string str has a
//token? if its true, then there is a token,
otherwise, you won't enter the loop.
{
System.out.println(str.next());
}

---------------------------the
output----------------------------------------------------------------
my
problems
are
gone!
-----------------------------------------------------------------------------------------------------
>>>"i stand to be corrected though"-but that what i know.

-----------------------------------------------------------------------------------------------------

sihle87
University of KwaZulu-Natal(South Africa)
Actuarial Student
-----------------------------------------------------------------------------------------------------
 
Reply With Quote
 
KyoGaSuki
Guest
Posts: n/a
 
      02-28-2008
Thank you both so much ^-^
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-29-2008
On Thu, 28 Feb 2008 01:34:31 -0800 (PST), KyoGaSuki
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>What is hasNext? I know you use it when you are reading from a file
>and it will stop when it has nothing more to read, but how would you
>use it? I have seen a couple of different examples of it, but all
>they do is make me more confused. I just can't seem to understand
>it...can anyone help?


see http://mindprod.com/jgloss/enumeration.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments