gpt4 book ai didi

Java在不同地方打印出抛出异常和不抛出异常

转载 作者:行者123 更新时间:2023-12-02 09:01:29 25 4
gpt4 key购买 nike

我不知道如何制定标题,但我遇到了以下问题。我正在制作一个程序,从 CSV 文件中读出内容并将其输入。在没有问题的文件中,我可以正常得到结果。当我运行另一个抛出错误的文件时,我的程序仍然打印出我在 catch 中的内容,但不是按照我想要的顺序打印 - 我想将所有有错误的部分放在上面,而那些没有错误的部分放在上面打印件的底部。我怎样才能做到这一点?这是我目前拥有的代码

           String []arrayS = line.split(",");
if(arrayS.length==7){
String fName = "";
String lName = "";
//String
int bDay;
int bMonth;
int bYear;
int weight;
double height;
try{
//System.out.printf("%15s%15s%15s%15s \n", fnlName , birthDate, sWeight, sHeight);

fName = arrayS[0];//gets first line in csv- name
lName = arrayS[1];//gets second line in csv- last name
try{
bDay = Integer.parseInt(arrayS[2].trim());//gets third line in csv- birth day
}catch (NumberFormatException nfe){
System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2] + ", " + arrayS[3] + ", " + arrayS[4] + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth day");
continue;
}try{
bMonth = Integer.parseInt(arrayS[3].trim());//gets four line in csv- birth month
}catch (NumberFormatException nfe){
System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2] + ", " + arrayS[3] + ", " + arrayS[4] + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth month");
continue;
}try{
bYear = Integer.parseInt(arrayS[4].trim());//gets fifth line in csv- birth year
}catch (NumberFormatException nfe){
System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2] + ", " + arrayS[3] + ", " + arrayS[4] + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Birth year");
continue;
}try{
weight = Integer.parseInt( arrayS[5].trim());//gets sixth line in csv- weight
}catch (NumberFormatException nfe){
System.err.println("Error found in" +arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2] + ", " + arrayS[3] + ", " + arrayS[4] + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Weight");
continue;
}try{
height = Double.parseDouble(arrayS[6].trim());//gets seventh line in csv- height
}catch (NumberFormatException nfe){
System.err.println("Error found in" + arrayS[0] + ", " + arrayS[1] + ", " + arrayS[2] + ", " + arrayS[3] + ", " + arrayS[4] + ", " + arrayS[5] + ", " + arrayS[6] +"\n Offending item is: Height");
continue;
}
System.out.printf("%15s%15s%02d/%02d/%4d %d %.1f\n" , fName , lName , bDay , bMonth , bYear , weight , height);

}catch(NumberFormatException nfe ){
System.out.println("Cannot read student:" + fName);
continue;
}}```

最佳答案

不要将输出直接写入 System.outSystem.err但首先要收集它们。您可以使用不同的方法,例如使用 List<String>或使用 StringBuilder 。当您完全读取 CSV 文件后,您终于可以输出最后收集的结果,首先是错误,然后是无错误的条目。代码可以如下所示:

List<String> errors = new ArrayList<String>();
List<String> output = new ArrayList<String>();

while (/*reading a line */) {
/* [...] */

try {
/* [...] */
output.add(String.format("...", a, b, c, d, ...);
} catch (...) {
errors.add(...);
}
}

// now show the result, but show the errors first
if (!errors.isEmpty()) {
System.err.println("There were errors:");
for (String str: errors) {
System.err.println(str);
}
}
System.out.println("Your result");
for (String str: output) {
System.out.println(str);
}

关于Java在不同地方打印出抛出异常和不抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com