How to read input with multiple lines in Java

后端 未结 10 1072
名媛妹妹
名媛妹妹 2020-12-05 02:48

Our professor is making us do some basic programming with Java, he gave a website and everything to register and submit our questions, for today I need to do this one exampl

相关标签:
10条回答
  • 2020-12-05 03:28

    Look into BufferedReader. If that isn't general/high-level enough, I recommend reading the I/O tutorial.

    0 讨论(0)
  • 2020-12-05 03:30

    A lot of student exercises use Scanner because it has a variety of methods to parse numbers. I usually just start with an idiomatic line-oriented filter:

    import java.io.*;
    
    public class FilterLine {
    
        public static void main(String[] args) throws IOException {
            BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));
            String s;
    
            while ((s = in.readLine()) != null) {
                System.out.println(s);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 03:34
     public class Sol {
    
       public static void main(String[] args) {
    
       Scanner sc = new Scanner(System.in); 
    
       while(sc.hasNextLine()){
    
       System.out.println(sc.nextLine());
    
      }
     }
    }
    
    0 讨论(0)
  • 2020-12-05 03:41

    The easilest way is

    import java.util.*;
    
    public class Stdio4 {
    
    public static void main(String[] args) {
        int a=0;
        int arr[] = new int[3];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        { 
             a = scan.nextInt();  //Takes input from separate lines
             arr[i]=a;
    
    
        }
        for(int i=0;i<3;i++)
        { 
             System.out.println(arr[i]);   //outputs in separate lines also
        }
    
    }
    

    }

    0 讨论(0)
  • 2020-12-05 03:43

    I finally got it, submited it 13 times rejected for whatever reasons, 14th "the judge" accepted my answer, here it is :

    import java.io.BufferedInputStream;
    import java.util.Scanner;
    
    public class HashmatWarrior {
    
        public static void main(String args[]) {
            Scanner stdin = new Scanner(new BufferedInputStream(System.in));
            while (stdin.hasNext()) {
                System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong()));
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 03:47

    This is good for taking multiple line input

    import java.util.Scanner;
    public class JavaApp {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            String line;
            while(true){
                line = scanner.nextLine();
                System.out.println(line);
                if(line.equals("")){
                    break;
                }
            }
        }  
    }
    
    0 讨论(0)
提交回复
热议问题