Java End-of-file

  • + 4 comments

    Hey could you please help me with this problem... I am using a while loop just like you but in the output the Hello part is not being printed..

        this is my code-->
        Scanner sc=new Scanner(System.in);
        String s=sc.next();   
        int i=1;
        while(sc.hasNextLine())
        {     String s1=sc.nextLine();
              System.out.println(i+" "+s1);
              i++;
        }
    
        Expected Output-->
        1 Hello world
        2 I am a file
        3 Read me until end-of-file.
    
        My output-->
        1  world
        2 I am a file
        3 Read me until end-of-file.