We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Introduction
- Java End-of-file
- Discussions
Java End-of-file
Java End-of-file
Sort by
recency
|
1109 Discussions
|
Please Login in order to post a comment
Scanner s1=new Scanner(System.in); int val=1; while(s1.hasNext()) { String word=s1.nextLine(); System.out.printf("%d %s\n",val,word); val++;
s1.close();
Using stream
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int lineNumber=1; while(sc.hasNext())// Reads Until EOF { String line= sc.nextLine(); System.out.println(lineNumber + " "+ line); lineNumber++; } } }