[英]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.