Java 捕获异常后继续读取文件
我得到了一个data.txt文件,它由数字组成Java 捕获异常后继续读取文件,java,file,io,bufferedreader,Java,File,Io,Bufferedreader,我得到了一个data.txt文件,它由数字组成 12 45 345 500 45.67684 33 一旦readData方法被执行,它应该只打印整数的值和对任何其他类型无效的打印元素,然后像这样继续 readData(“data.txt”)将 打印以下内容: 12 45 345 500元素无效33 我的问题是,一旦打印出element not valid语句,我的代码就不会继续打印33,它只会在12 45 345 500 element not valid处停止 import java.io.*
12 45 345 500 45.67684 33 一旦readData方法被执行,它应该只打印整数的值和对任何其他类型无效的打印元素,然后像这样继续 readData(“data.txt”)将 打印以下内容: 12 45 345 500元素无效33 我的问题是,一旦打印出element not valid语句,我的代码就不会继续打印33,它只会在12 45 345 500 element not valid处停止
import java.io.*;
import java.util.*;
public class Ex7 {
public static void readData(String nameFile){
try{
BufferedReader br = new BufferedReader(new FileReader(nameFile));
String line = br.readLine();
int i = 0;
while((line != null)){
try{
String[] lines = line.split(" ");
int[] result = new int[lines.length];
result[i] = Integer.parseInt(lines[i]);
if(result[i] ==(int)result[i]){
System.out.print(result[i] + " ");
}
i++;
}
}
}catch(NumberFormatException e){
System.out.println("element not valid " );
}catch(IOException e){
System.out.println("IO error");
System.exit(0);
}
}
public static void main (String[] args){
readData("data.txt");
}
}
我建议不要使用Exception,而是检查它是否是一个数字。异常的速度很慢,只能用作后备。有关数字字符串的更多信息,请查看此链接,或通过谷歌搜索其他方式。
我建议不要使用Exception,而是检查它是否是一个数字。异常的速度很慢,只能用作后备。有关数字字符串的更多信息,请查看此链接,或通过谷歌搜索其他方式。
将catch块捕获到循环中的No.MultFrimeExtExchange,同时也考虑由@α-ySuthOrth.A*yx*给出的答案。
将catch块捕获在循环中的NoMultFrimeExtExchange,但也要考虑由@ AythySuthOrth.A*Y.*给出的答案。
< P>你的问题的原因是你不这样做。了解发生的控制流程 这很简单:您有一个从文件读取的循环。但是你的捕获在这个循环之外。所以,当有东西抛出时,你在循环外“捕捉”到,这个循环就“结束”;没有办法回去了 因此,直接的答案是:移动NumberFormatException的try/catch。。。到一个实际可能发生这种情况的地方:try {
result[i] = Integer.parseInt(lines[i]);
} catch (NumberFormatException ...
但是当然。。。这直接导致了代码的下一个问题:您正在使用数组存储“有效”输入。。。数组具有固定大小。您如何提前知道有多少成员有效?你没有。因此,您必须从使用数组更改为动态列表,如ArrayList。现在您可以添加“有效”行。但无论如何,这并不重要。因为您的方法是而不是返回收集的值。但如果没有使用数据;一开始收集它是没有意义的
除了你必须修改代码之外,答案是:回到书本上;和了解您正在使用的结构/概念实际上是如何工作的。盲目地键入try/catch是没有意义的,仅仅因为它们出现在某个地方,或者因为您的IDE告诉您需要以某种方式处理异常。意思:当你写下代码时。。。确保您真正理解这段代码的作用。你知道,就像另一个语句
if(result[i]=(int)result[i])
。。。这永远是真的;根本没有任何意义 你提出问题的原因是你不了解发生的控制流程
这很简单:您有一个从文件读取的循环。但是你的捕获在这个循环之外。所以,当有东西抛出时,你在循环外“捕捉”到,这个循环就“结束”;没有办法回去了
因此,直接的答案是:移动NumberFormatException的try/catch。。。到一个实际可能发生这种情况的地方:
try {
result[i] = Integer.parseInt(lines[i]);
} catch (NumberFormatException ...
但是当然。。。这直接导致了代码的下一个问题:您正在使用数组存储“有效”输入。。。数组具有固定大小。您如何提前知道有多少成员有效?你没有。因此,您必须从使用数组更改为动态列表,如ArrayList。现在您可以添加“有效”行。但无论如何,这并不重要。因为您的方法是而不是返回收集的值。但如果没有使用数据;一开始收集它是没有意义的
除了你必须修改代码之外,答案是:回到书本上;和了解您正在使用的结构/概念实际上是如何工作的。盲目地键入try/catch是没有意义的,仅仅因为它们出现在某个地方,或者因为您的IDE告诉您需要以某种方式处理异常。意思:当你写下代码时。。。确保您真正理解这段代码的作用。你知道,就像另一个语句
if(result[i]=(int)result[i])
。。。这永远是真的;根本没有任何意义 将NumberFormatException放在While中,While应该可以工作
public static void readData(String nameFile){
try{
BufferedReader br = new BufferedReader(new FileReader(nameFile));
String line = br.readLine();
int i = 0;
while((line != null)){
try{
String[] lines = line.split(" ");
int[] result = new int[lines.length];
result[i] = Integer.parseInt(lines[i]);
if(result[i] ==(int)result[i]){
System.out.print(result[i] + " ");
}
i++;
}catch(NumberFormatException e){
System.out.println("element not valid " );
}
}
}catch(IOException e){
System.out.println("IO error");
System.exit(0);
}
}
将NumberFormatException放在While中,While应该可以工作
public static void readData(String nameFile){
try{
BufferedReader br = new BufferedReader(new FileReader(nameFile));
String line = br.readLine();
int i = 0;
while((line != null)){
try{
String[] lines = line.split(" ");
int[] result = new int[lines.length];
result[i] = Integer.parseInt(lines[i]);
if(result[i] ==(int)result[i]){
System.out.print(result[i] + " ");
}
i++;
}catch(NumberFormatException e){
System.out.println("element not valid " );
}
}
}catch(IOException e){
System.out.println("IO error");
System.exit(0);
}
}
使用以下代码:
public static void readData(String nameFile) {
try {
BufferedReader br = new BufferedReader(new FileReader(nameFile));
String line = br.readLine();
String[] lines = line.split(" ");
int[] result = new int[lines.length];
for(int i=0;i<lines.length;i++){
try{
result[i] = Integer.parseInt(lines[i]);
if(result[i] ==(int)result[i]){
System.out.print(result[i] + " ");
}
}catch(NumberFormatException nfe){
System.out.println("Number Invalid");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
publicstaticvoidreaddata(stringnamefile){
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(名称文件));
String line=br.readLine();
字符串[]行=行。拆分(“”);
int[]结果=新的int[lines.length];
对于(int i=0;i使用以下代码:
public static void readData(String nameFile) {
try {
BufferedReader br = new BufferedReader(new FileReader(nameFile));
String line = br.readLine();
String[] lines = line.split(" ");
int[] result = new int[lines.length];
for(int i=0;i<lines.length;i++){
try{
result[i] = Integer.parseInt(lines[i]);
if(result[i] ==(int)result[i]){
System.out.print(result[i] + " ");
}
}catch(NumberFormatException nfe){
System.out.println("Number Invalid");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
publicstaticvoidreaddata(stringnamefile){
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(名称文件));
String line=br.readLine();
字符串[]行=行。拆分(“”);
int[]结果=新的int[lines.length];
对于(int i=0;i)您的NumberFormatException是在while循环之外捕获的,我建议您验证try-、while-和catch-语句的括号。您的try-catch无效,您有两个try-one未关闭。此外,如果不想对代码进行重大更改,您应该在while循环本身中选择then NumberFormatException。顺便说一句,while((line!=null))当您在while内部处理异常时,这将永远不会退出,您必须这样做。如果您的缩进不是那么随意,那么可以直接看到哪些块在内部