Java,从input.txt读取文件时出现问题
我正在做这个项目。出于我的目的,我需要使用它们来查找中间值 在我这一点上,我需要看看阅读 我还创建了input.txt,如下所示Java,从input.txt读取文件时出现问题,java,Java,我正在做这个项目。出于我的目的,我需要使用它们来查找中间值 在我这一点上,我需要看看阅读 我还创建了input.txt,如下所示 3 7 1 4 5 7 9 11 13 在代码段下面,我为readpath创建了变量 // need the variable of filename to read private static final String INPUT_FILE_PATH = "input.txt"; 然后,我在main函数中添加了需要读取input.txt中的数字整数的代
3 7
1 4 5 7 9 11 13
在代码段下面,我为readpath创建了变量
// need the variable of filename to read
private static final String INPUT_FILE_PATH = "input.txt";
然后,我在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
}
publicstaticvoidmain(字符串参数[]){
//读取输入文件
//TODO需要修复此获取错误输入的readpath
//!尽快
Path inputPath=Path.get(输入文件路径);
Charset Charset=Charset.forName(“UTF-8”);
列表文件行=新的ArrayList(0);
试一试{
fileLines=Files.readAllLines(输入路径,字符集);
}捕获(IOEX异常){
System.err.println(“读取文件时出错:+ex.getMessage());
系统出口(1);
}
int read_line=0;
试一试{
read_line=Integer.parseInt(fileLines.get(0));
}捕获(NumberFormatException ex){
System.err.println(“错误的文件输入”);
系统出口(1);
}
System.out.println(“读取…”+读取行);
//读取文件名操作结束
}
因此,这段代码应该可以工作。我得到的输出是错误的文件输入。我不明白为什么会有坏文件。顺便说一下,我把所有文件放在同一个目录中
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);
}
该行包含两个数字,并导致NumberFormatException。通过放置两条打印语句,您应该能够轻松地发现问题。从输出中可以看出,
fileLines.get(0)
返回37
,无法转换为int
值。因此,首先需要将它们存储到单独的String
变量中,然后对每个变量应用Integer.parseInt
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
static String INPUT_FILE_PATH="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<String>(0);
try {
fileLines = Files.readAllLines(inputPath, charset);
System.out.println("Content of all lines: "+fileLines);
} catch (IOException ex) {
System.err.println("Error reading file: " + ex.getMessage());
System.exit(1);
}
int read_line = 0;
try {
System.out.println("Content of first line: "+fileLines.get(0));
String []numsInFirstLine=fileLines.get(0).split(" ");
int num1=Integer.parseInt(numsInFirstLine[0]);
int num2=Integer.parseInt(numsInFirstLine[1]);
System.out.println(num1);
System.out.println(num2);
} catch (NumberFormatException ex) {
System.err.println("bad file input");
System.exit(1);
}
System.out.println("reading... " + read_line);
// end of reading the filename operation
}
}
我建议您在IDE中使用调试器来避免这种情况。错误消息是什么?异常是什么?
read\u line=Integer.parseInt(fileLines.get(0))
parseInt需要一个整数。不是一大堆。打印实际的异常消息。这会对你有所帮助。我不会在这里打印“错误的文件输入”。您没有收到“错误的文件输入”,您收到的是NumberFormatException,因为您正试图将“37”转换为整数-您不能这样做,因为有空格。@Fildor谢谢。我去看看。
Content of all lines: [3 7, 1 4 5 7 9 11 13]
Content of first line: 3 7
3
7
reading... 0