Submitting code on CodeEval site

后端 未结 3 1749
难免孤独
难免孤独 2021-01-21 00:22

A Java program on CodeEval had to accept a file path as an argument. I used a command line argument to do this, but I get an exception as below when I submitted my code on CodeE

3条回答
  •  情歌与酒
    2021-01-21 01:14

    Here's the boilerplate Java code that I use for my Codeeval code. The specific problem code generally goes in the processLine method. I don't use Scanner or StringTokenizer. I use the String split method to process the input.

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Main implements Runnable {
    
        private String fileName;
    
        public Main (String fileName) {
            this.fileName = fileName;
        }
    
        @Override
        public void run() {
            try {
                processFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void processFile() throws IOException {
            BufferedReader br = new BufferedReader(
                    new FileReader(fileName));
            String line = "";
            while ((line = br.readLine()) != null) {
                processLine(line);
            }
            br.close();
        }
    
        private void processLine(String line) {
            System.out.println(line);
        }
    
        public static void main(String[] args) {
            new Main(args[0]).run();
        }
    
    }
    

提交回复
热议问题