Java End-of-file

Sort by

recency

|

1109 Discussions

|

  • + 0 comments

    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();

  • + 1 comment

    Using stream

    import java.util.*;
    import java.util.stream.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            IntStream.iterate(1, n -> n + 1)
                     .takeWhile(n -> sc.hasNextLine())
                     .forEach(n -> System.out.println(n + " " + sc.nextLine()));
            sc.close();
        }
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int i = 1;
            while(scanner.hasNext()){
                String s = scanner.nextLine();
                System.out.println(i +" "+ s);
                i++;            
            }
        }
    }
    
  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
              int i = 0;
           while(scanner.hasNext()){
               String frase = scanner.nextLine();
                i++;
                   System.out.println(i + " " + frase);
               }
           scanner.close();
        }
    }
    
  • + 1 comment

    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++; } } }