gpt4 book ai didi

java - 未处理的异常类型 Exception (Java/Weka) 的 Eclipse 自动建议

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

我正在尝试编写一个可以在Weka中批量处理多个ARFF文件的脚本。但是我总是收到“未处理的异常类型异常”错误。 Eclipse 建议在每一行代码周围放置 try-catch 语句。

接受建议后,代码将运行。然而,如果我这样做的话,这是非常难以阅读的代码。有谁知道如何修复此未处理的异常类型异常错误吗?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;
import weka.classifiers.trees.RandomForest;


public class Eval{
public static Instances LoadARFF(String location){
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(location));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArffReader arff = null;
try {
arff = new ArffReader(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arff.getData();
}

public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);

//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");

//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;

//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);

//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);

//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);

// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
} //if
});//for each
}//Main
}//class

错误代码如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Unhandled exception type Exception
Unhandled exception type Exception
Unhandled exception type Exception

at Eval.main(Eval.java:60)

接受 Eclipse 自动建议后,代码大约是接受之前的两倍。

最佳答案

我会做类似的事情

private static void processFile(Path filePath) {
System.out.println(filePath);

//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");

//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;

//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);

//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);

//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);

// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
}

private static void processPath(Path path) {
if(Files.isRegularFile(path)) {
try {
processFile(path);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}

public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(Eval::processPath);
}//Main

关于java - 未处理的异常类型 Exception (Java/Weka) 的 Eclipse 自动建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37707048/

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