简体   繁体   English

Java,从input.txt读取文件有问题

[英]Java, reading the file from input.txt has an issue

I am working on the project.我正在做这个项目。 For my purpose, I need to use them that find the median of medians.出于我的目的,我需要使用它们来找到中位数。

At my point, I need to see the read在我看来,我需要看看阅读

I also created the input.txt like that below我还创建了如下所示的 input.txt

3 7
1 4 5 7 9 11 13

Below the snippet, I created the variable for the readpath.在代码片段下方,我为读取路径创建了变量。

// need the variable of filename to read
    private static final String INPUT_FILE_PATH = "input.txt";

So, then I appended the code that needs to read the numerical integers in the input.txt in the main function as known below所以,然后我在 main 函数中附加了需要读取 input.txt 中的数字整数的代码,如下所示

public static void main(String args[]){
        // read the input file
        // TODO need to fix this readpath that gets the bad input
        // ! ASAP
        Path inputPath = Paths.get(INPUT_FILE_PATH);
        Charset charset = Charset.forName("UTF-8");
        List<String> fileLines = new ArrayList<>(0);
        try {
            fileLines = Files.readAllLines(inputPath, charset);
        } catch (IOException ex) {
            System.err.println("Error reading file: " + ex.getMessage());
            System.exit(1);
        }
        int read_line = 0;
        try {
            read_line = Integer.parseInt(fileLines.get(0));
        } catch (NumberFormatException ex) {
            System.err.println("bad file input");
            System.exit(1);
        }
        System.out.println("reading... " + read_line);
        // end of reading the filename operation
}

As a result, this code suppose to work.因此,这段代码应该可以工作。 I get the output that is bad file input.我得到的输出是错误的文件输入。 I do not understand why it gets bad file.我不明白为什么它会得到坏文件。 By the way, I put all files together in the same directory.顺便说一下,我将所有文件放在同一个目录中。

    int read_line = 0;
    int read_line2 = 0;
    try {
        String[] words = fileLines.get(0).split("\\s+"); // Split on whitespace.
        read_line = Integer.parseInt(words[0]);
        read_line2 = Integer.parseInt(words[1]);
    } catch (NumberFormatException ex) {
        System.err.println("bad file input - not a number; " + e.getMessage());
        System.exit(1);
    }

The line contains two numbers, and results in a NumberFormatException.该行包含两个数字,并导致 NumberFormatException。

I am working on the project.我正在做这个项目。 For my purpose, I need to use them that find the median of medians.出于我的目的,我需要使用它们来找到中位数的中位数。

At my point, I need to see the read就我而言,我需要看读

I also created the input.txt like that below我还像下面那样创建了input.txt

3 7
1 4 5 7 9 11 13

Below the snippet, I created the variable for the readpath.在代码段下方,我为readpath创建了变量。

// need the variable of filename to read
    private static final String INPUT_FILE_PATH = "input.txt";

So, then I appended the code that needs to read the numerical integers in the input.txt in the main function as known below因此,然后我在主函数中的input.txt中附加了需要读取数字整数的代码,如下所示

public static void main(String args[]){
        // read the input file
        // TODO need to fix this readpath that gets the bad input
        // ! ASAP
        Path inputPath = Paths.get(INPUT_FILE_PATH);
        Charset charset = Charset.forName("UTF-8");
        List<String> fileLines = new ArrayList<>(0);
        try {
            fileLines = Files.readAllLines(inputPath, charset);
        } catch (IOException ex) {
            System.err.println("Error reading file: " + ex.getMessage());
            System.exit(1);
        }
        int read_line = 0;
        try {
            read_line = Integer.parseInt(fileLines.get(0));
        } catch (NumberFormatException ex) {
            System.err.println("bad file input");
            System.exit(1);
        }
        System.out.println("reading... " + read_line);
        // end of reading the filename operation
}

As a result, this code suppose to work.结果,此代码假定可以正常工作。 I get the output that is bad file input.我得到的输出是错误的文件输入。 I do not understand why it gets bad file.我不明白为什么它会得到错误的文件。 By the way, I put all files together in the same directory.顺便说一句,我将所有文件放在同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从input.txt文件创建String数组 - How to create a String array from an input.txt file 尝试在Java中将input.txt文件转换为output.txt文件 - Trying to utilize an input.txt file into a output.txt file in Java 在Java中更改Input.txt文件中的字符串而不创建Output.txt - Change String in the Input.txt File in Java without creating Output.txt 读取input.txt后,扫描程序没有发现Line异常 - Scanner throws no Line found exception after reading a input.txt 从input.txt中将新数据读入名称数组 - read new data into array of names from input.txt 如何获取 input.txt 文件中已经存在的数据并使用 Map 找到具有部门名称的最高薪水<String,Integer>在java中? - How to get data that is already present in input.txt file and find the highest salary with Department name using a Map<String,Integer> in java? 从 a.txt/输入文件中读取数字并在 Java Eclipse 中打印一个三角形 - Reading numbers from a .txt/input file and printing a triangle in Java Eclipse 在Java中从.txt文件读取和保存行问题 - Issue reading and saving lines from .txt file in Java 运行外部“java myprog &lt;input.txt&gt; output.txt”的Java程序 - A Java program that runs an external “java myprog < input.txt > output.txt” 从Java中的txt文件读取 - Reading from a txt file in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM