“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:"&引用;在爪哇
我用java创建了一个类来读取文本文件(.txt)并在屏幕上打印结果。脚本正在读取文本文件的内容,但最后显示消息:“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:"&引用;在爪哇,java,Java,我用java创建了一个类来读取文本文件(.txt)并在屏幕上打印结果。脚本正在读取文本文件的内容,但最后显示消息: Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at com.desafioProgramacao.LerArquivo.main(LerArquivo.java:24)
我不知道它为什么会显示此消息。在FINALLY类中,我告诉它如果文件的内容为null,则关闭FileReader和BufferedReader。遵循Java代码和屏幕打印
public class LerArquivo {
private static final String NomeArquivo = "E:\\DesafioProgramacao\\matriculasSemDV.txt";
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(NomeArquivo);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}}
您的文件包含一个空行(可能在末尾) 将while循环替换为:
while ((sCurrentLine = br.readLine()) != null && !sCurrentLine.isEmpty())
您的文件包含一个空行(可能在末尾) 将while循环替换为:
while ((sCurrentLine = br.readLine()) != null && !sCurrentLine.isEmpty())
表面上的原因是您读取了一个空字符串,并希望将其解析为
int
对于代码,您需要检查sCurrentLine
值
while ((sCurrentLine = br.readLine()) != null) {
if(StringUtils.isNotBlank(sCurrentLine)){//StringUtils is from `commons-lang`
// or if(sCurrentLine.length()>0)
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
}
}
对于txt文件,您需要删除文件末尾的所有空行表面原因是您读取了一个空字符串,并希望将其解析为
int
对于代码,您需要检查sCurrentLine
值
while ((sCurrentLine = br.readLine()) != null) {
if(StringUtils.isNotBlank(sCurrentLine)){//StringUtils is from `commons-lang`
// or if(sCurrentLine.length()>0)
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
}
}
对于txt文件,您需要删除文件末尾的所有空行问题是最后一行,它是一个空格。你可以做:
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
}
}
问题是最后一行,它是一个空白。你可以做:
while ((sCurrentLine = br.readLine()) != null) {
if (!sCurrentLine.isEmpty()) {
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
}
}
解决此问题的正确方法是捕获NumberFormatException并正确处理它,如下所示:
try {
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
} catch (NumberFormatException ex) {
System.out.println("Error reading line: " + sCurrentLine);
}
解决此问题的正确方法是捕获NumberFormatException并正确处理它,如下所示:
try {
int num = Integer.parseInt(sCurrentLine);
System.out.println(num);
} catch (NumberFormatException ex) {
System.out.println("Error reading line: " + sCurrentLine);
}
看起来您的文本文件末尾有一个额外的行。可能您的文件末尾有一个空行看起来您的文本文件末尾有一个额外的行。可能您的文件末尾有一个空行为什么不
!sCurrentLine.isEmpty()
?对于这样一个简单的问题,不需要涉及Apache Commons。因为需要检查它是否有多个空空间,并且我不确定sCurrentLine.isEmpty()
是否可以检查它为什么不调用parseInt(..)捕获该异常,而不是提前考虑所有可能的情况?为什么不!sCurrentLine.isEmpty()
?对于这样一个简单的问题,不需要涉及Apache Commons。因为需要检查它是否有多个空空间,并且我不确定sCurrentLine.isEmpty()
是否可以检查它为什么不调用parseInt(..)捕获该异常,而不是提前考虑所有可能的情况?