Java End-of-file

Sort by

recency

|

1157 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner myObj = new Scanner(System.in);
            for(int i=1;myObj.hasNext();i++) {
                if(myObj.hasNext() == true) {
                     System.out.println(i + " " + myObj.nextLine());
                } else{
                    break;
                }
            }
        }
    }
    
  • + 0 comments

    Here's a very simple solution:

    import java.util.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int line = 0;
        while(scanner.hasNext()){
            String input = scanner.nextLine();
            line++;
            System.out.println(line + " " + input);
        }
        scanner.close();
    }
    

    }

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int line = 0;
            while (sc.hasNext()){
                String input = sc.nextLine();
                line += 1;
                System.out.println(line+" "+input);
            }
            sc.close();
        }
    }
    
  • + 0 comments
    while (sc.next()){
    System.out.println(i++ +" "+ sc.nextLine());
    }
    sc.close();
    
  • + 0 comments

    Scanner sc=new Scanner(System.in);

        for(int line=1; sc.hasNext(); line++){
            System.out.println(line+" "+sc.nextLine());
        }
        sc.close();