reading input till EOF in java

后端 未结 10 628
后悔当初
后悔当初 2020-12-09 06:35

In C++ if I wish to read input till the EOF I can do it in the following manner

while(scanf(\"%d\",&n))
{
    A[i]=n;
    i++;
}

I can

相关标签:
10条回答
  • 2020-12-09 06:38

    Here is my Java equivalent code to read input until End-of-File:

    import java.util.Scanner;
    
    public class EndOfFileSolutions {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            for(int i = 1; sc.hasNext()== true; i++){
                System.out.println(i + " " + sc.nextLine());
            }
        }
    }
    

    This code will produce output look like below --

    Sample Input:

    Hello world
    I am a file
    Read me until end-of-file.
    

    Sample Output:

    1 Hello world
    2 I am a file
    3 Read me until end-of-file.
    

    This answer also works fine to solve Hackerrank EOF problem

    0 讨论(0)
  • 2020-12-09 06:38

    A simple solution would be to use Scanner class.

    See snipet below:

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            while(s.hasNextLine())
            {
                String line = s.nextLine();
                System.out.println(line);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 06:42

    The only thing that really works for me (you don't even have to create a file)

    Scanner read = new Scanner(System.in);
    String cadena;
    boolean cond = true;
    int i =0;
    while (cond){
        cadena = read.nextLine();
        if(cadena.isEmpty())
            cond = false;
    }
    
    0 讨论(0)
  • 2020-12-09 06:43

    In Java, One way of reading till EOF is:-

         Scanner scan = new Scanner(System.in);    
         while(scan.hasNextLine())   //read until condition is true
         {
            String line = scan.nextLine();
            System.out.println(line);
            i++;
         } 
    
    0 讨论(0)
  • 2020-12-09 06:44

    Here is Java equivalent code using BufferedReader and FileReader classes.

      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
    
      public class SmallFileReader {
            public static void main(String[] args) throws IOException {  
    

    Option 1:
    String fileName = args[0];
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    Option 2:
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a file name: ");
    String fileName = br.readLine();

                 //BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
                 String line=null;
                 while( (line=br.readLine()) != null) {
                        System.out.println(line);  
                 }
      }
    }  
    

    I made little modification to @Vallabh Code. @tom You can use the first option, if you want to input the file name through command line.
    java SmallFileReader Hello.txt
    Option 2 will ask you the file name when you run the file.

    0 讨论(0)
  • 2020-12-09 06:45

    Here is Java equivalent code using BufferedReader and FileReader classes.

      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
    
      public class SmallFileReader {
            public static void main(String[] args) throws IOException {
                 BufferedReader br = new BufferedReader(new FileReader("Demo.txt"));
                 String line=nul;
                 while( (line=br.readLine()) != null) {
                        System.out.println(line);  
                 }
      }
    }
    
    0 讨论(0)
提交回复
热议问题