Java String Tokens

  • + 2 comments

    in normal scenario when you dont set the delimiter as \z

    scan.next()
    

    will read input till it encounters \n (i.e. new line character)

    and once it reads \n it stops! which means \n acts as the

    fullstop (or Delimiter).

    But in the given problem our input has multiple lines and

    we have to read them all in one single string

    i.e. we have to also read lines that appear after \n without stopping.

    so when we use

    scan.useDelimiter("\\Z");
    

    this tells scan to use \Z as Delimiter instead of \n (default) for scan.next()

    now meaning of \Z is end of the input or the final terminator

    i.e. scan.next() will not stop reading until entire input is read.