What does hasNext() in a While do?

I’m new to JAVA and I’m trying to read data from a .txt file. I’ve implemented a Scanner object ‘in’ Scanner in=new Scanner("file.txt"); and then down the code a while loop to traverse through the content of the file.There are multiple rows of data in the file and in each row there are three strings (security number ,First name ,Last name : 01 Thomas Anderson)

while(in.hasNext()){
    String ss = in.next();
    String FName=in.next();
    String LName=in.next();

    System.out.printf("SSN: %s, FirstName: %s, LastName: %s \n",ss,FName,LName);
    }

So,what does the hasNext() method do? Does it look for end of the row or for the last row in the file or…. ?

PLEASE elaborate on the working of the above snippet (mentioning the next() method too)

🙂

Leave a Comment