Java End-of-file

  • + 2 comments

    import java.util.*; public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i=1; sc.hasNext(); i++) {      
            String line = sc.nextLine();
            System.out.println(i + " "+line);;
        }
        sc.close();
    }
    

    }

    // sc.hasNext() causes program to wait, as it checks that there is nothing in input buffer // so it waits that user might enter something, user enters, it check that input is now // available, so it enters the loop. now sc.nextLine() assigns the input (which is in buffer) // to 'line'