Java End-of-file

  • + 2 comments

    hey as i see here many of us didn't understand what actually " .hasNextLine()" function do so let me try to explain think it as a train ticket. you purchased a ticket to go somewhere and its pretty obvious that after reaching your stop that ticket will not be useful for you....as well as here when user input a string that time think it as you booked your train you purchased your ticket and after printing that string you reached your destination means what suppose 's' is a variable which has "hey this is me " by user okey and for input you created a scanner object as you know then scanner has a token for that string so it will return you true that time but whem you press enter that means you reached your stop so after printing it or encounter a enter it will return you false

    hope u find it helpful here is my code it works fine

    public class Solution {

    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);  
        int i = 0;
        while (sc.hasNextLine()) {
            System.out.printf("%d %s\n",++i,sc.nextLine());
        }
    }
    

    }