hasNext?

Discussion in 'Java' started by KyoGaSuki, Feb 28, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    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?
     
    KyoGaSuki, Feb 28, 2008
    #1
    1. Advertising

  2. KyoGaSuki <> 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() .
     
    Andreas Leitgeb, Feb 28, 2008
    #2
    1. Advertising

  3. KyoGaSuki

    Guest

    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
    -----------------------------------------------------------------------------------------------------
     
    , Feb 28, 2008
    #3
  4. KyoGaSuki

    KyoGaSuki Guest

    Thank you both so much ^-^
     
    KyoGaSuki, Feb 28, 2008
    #4
  5. KyoGaSuki

    Roedy Green Guest

    On Thu, 28 Feb 2008 01:34:31 -0800 (PST), KyoGaSuki
    <> 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
     
    Roedy Green, Feb 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark van de Veerdonk

    Struts <logic:iterate> & hasNext ???

    Mark van de Veerdonk, Dec 15, 2003, in forum: Java
    Replies:
    5
    Views:
    6,439
    Sudsy
    Dec 16, 2003
Loading...

Share This Page