Java End-of-file

Sort by

recency

|

1170 Discussions

|

  • + 0 comments

    The task may look simple at first, but it really teaches you how to use loops and scanners effectively in Java. It’s also a good way to practice formatting output while processing multiple lines of input. Winstar exchange

  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
    
        int i=1;
        String str = "";
        while(in.hasNext()){
            str += "" + (i++) + " " + in.nextLine() + "\n";
        }
        System.out.println(str);
    }
    

    }

  • + 0 comments

    All recent soluitions I see here seem to ignore the requirement of reading all of the input first and only then printing the output. Such an approach is obviously more performant, but in real world requirements are there for a reason and unauthorized 'improvements' can lead to bugs.

  • + 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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner sc = new Scanner(System.in);
         int i=0;
         while(sc.hasNext()){
            i++;
            System.out.println(i+" "+sc.nextLine());
         }
         sc.close();
    }
    

    }

  • + 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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner sc = new Scanner(System.in);
         int i=0;
         while(sc.hasNext()){
            i++;
            System.out.println(i+" "+sc.nextLine());
         }
         sc.close();
    }
    

    }