java异常消息以无限循环打印
我一直在编写代码,在读取CSV后手动打印json(没有json库)。这是为了理解这种规模的项目的结构,它涉及IO和异常处理
所以,我从昨天开始调试这段代码。我已经发现了问题,但还没能解决
当前,在打印异常消息后,我有一个无限循环。对于上下文,在缺少的代码部分,用户输入文件数,并创建一个文件数组。我的消息中的代码片段是一种验证csv是否有空字段的方法。Count是用来计算行号的,如果Count==1,并且有一个空字段,它将抛出CSVFileInvalidException
,这是我为这个项目做的一个自定义异常。如果count大于1,则抛出CSVDataMissingException
,即另一个自定义异常
调试的时间表如下所示:
起初,当我使用无限循环时,我认为是正则表达式导致了我的错误,因为我听说它的实现可能很糟糕。我选中了,每个元素都是我想要的(一个字符串,代表csv中的一个字段)。所以,这不是正则表达式,因为这种行为是正常的
其次,我意识到我不小心把try块放在了while循环的内部,所以我把它放在了外部。不走运。这个问题仍然存在
然后,我发现了问题所在。它读取处理异常的行,因为没有故障保护退出while循环,所以它不停地退出。因此,我为while循环添加了一个退出条件。所以,我补充道
if ((count > 1) && (s.nextLine() == null || s.nextLine().equals(""))) {
break;
}
这将检查行号是否大于1,以及下一行是否为null或空。我以为这解决了我遇到的无限循环问题。一旦我实现了这一点,正则表达式被输出后就什么都没有了。我检查了多个println()
然后我意识到我在其中一个for循环中打错了。现在,我又回到了之前的行为。它再次在while循环中重做相同的序列。在本例中,它循环来自CSVFileInvalidException
的消息,即"The file called : " + f[i] + " is invalid. A field is missing. The file is not converted to JSON."
所以,我假设有些东西在逻辑上是不合理的。我尝试过多种解决方案,但现在都没有例外
抱歉,代码太长了,重复的元素在底部,其他所有内容都在while循环中,所以我不确定它是否是我在那里实现的
for (int i = 0; i < f.length; i++) {
int count = 1;
try {
while (s.hasNextLine()) {
lines = s.nextLine();
if ((count > 1) && (s.nextLine() == null || s.nextLine().equals(""))) {
break;
}
//isolate the commas, and remove the quotes and extra space, so that the text remains clean and similar
String[] line_parts = lines.replaceAll("^\"", "").split("\"?(,|$)(?=(([^\"]*\"){2})*[^\"]*$) *\"?", -1);
Cloned_Elements[] cloned_elements_array = new Cloned_Elements[line_parts.length];
for (int z = 0; z < cloned_elements_array.length; z++) {
String[] array_ofstring = new String[line_parts.length];
for (int x = 0; x < array_ofstring.length; x++) {
array_ofstring[x] = "lol";
}
cloned_elements_array[z] = new Cloned_Elements(array_ofstring);
}
for (int j = 0; j < line_parts.length - 1; j++) {
if (count == 1) {
if (line_parts[j].equals("") || line_parts[j] == null) {
ce.setCloned_array(line_parts);
throw new CSVFileInvalidException("The file called : " + f[i] + " is invalid. A field is missing. The file is not converted to JSON.");
} else {
if (j < line_parts.length) {
String[] length_1 = cloned_elements_array[j].getCloned_array();
cloned_elements_array[j].setCloned_array(line_parts);
}
}
} else {
if (line_parts[j].equals("") || line_parts[j] == null) {
ce.setCloned_array(line_parts);
throw new CSVDataMissingException("In file " + f[i] + ", there is missing data in line #" + (count) + ". Thus, the file is not converted to JSON.");
} else {
if (j < line_parts.length) {
cloned_elements_array[j].setCloned_array(line_parts);
}
}
}
}
count++;
}
} catch (CSVDataMissingException e2) {
System.err.println(e2.getMessage());
try {
p = new PrintWriter(new FileOutputStream(js[js.length - 1], true));
int line_nb = count+1;
String[] error_array = ce.resize_string_arr();
for (int x = 0; x < error_array.length; x++) {
if (error_array[x].equals("") || error_array[x] == null) {
error_array[x] = "xxx";
}
}
p.println("In file " + f[i] + " ,line " +line_nb + " is missing an element.");
for (int z = 0; z < error_array.length; z++) {
p.print(error_array[z] + " ");
}
p.println();
p.println();
break;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (CSVFileInvalidException e1) {
System.err.println(e1.getMessage());
try {
p = new PrintWriter(new FileOutputStream(js[js.length - 1], true));
p.println("File " + f[i] + " is invalid.");
String[] error_array = ce.resize_string_arr();
for (int x = 0; x < error_array.length; x++) {
if (error_array[x].equals("") || error_array[x] == null) {
error_array[x] = "xxx";
}
}
p.println("Field is missing : " + error_array.length + " detected, 1 is missing.");
for (int z = 0; z < error_array.length; z++) {
p.print(error_array[z] + " ");
}
p.println();
p.println();
break;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
共 (0) 个答案