我有一个方法,我在其中创建一个arff文件,如果我运行它(使用main方法中的代码)按预期工作。当我尝试将代码放入其他方法,然后从其他类(在我的情况下,从servlet类)调用该方法时,我在调用构造函数时从上面得到错误。我的wekka jar是3.8.1,FastVector类被删除了。任何想法如何解决这个问题?这是代码:
public void createTestData(int id_student, String teacher_subject, int id_level, boolean isMand)
throws IOException {
FastVector attribute, attributeVals;
Instances data;
Instances dataRel;
double[] vals;
String isMandatory = "" + isMand;
int level = 10;
String teacher = teacher_subject.split("-")[1];
String subject = teacher_subject.split("-")[0];
// 1. set up attributes
attribute = new FastVector();
attribute.addElement(new Attribute("id_student"));
// - string
attribute.addElement(new Attribute("subject", (FastVector) null));
attribute.addElement(new Attribute("teacher", (FastVector) null));
// - numeric
attribute.addElement(new Attribute("level"));
// - nominal
attributeVals = new FastVector();
attributeVals.addElement("true");
attributeVals.addElement("false");
attribute.addElement(new Attribute("isMandatory", attributeVals));
// attribute.addElement(new Attribute("rezult"));
// 2. create Instances object
data = new Instances("MyRelation", attribute, 0);
// 3. fill with data
// first instance
vals = new double[data.numAttributes()];
// - numeric
vals[0] = id_student;
// - nominal
vals[1] = data.attribute(1).addStringValue(subject);
// - string
vals[2] = data.attribute(2).addStringValue(teacher);
// - date
vals[3] = level;
vals[4] = attributeVals.indexOf(isMandatory);
// vals[5] = 10;
// add
data.add(new DenseInstance(1.0, vals));
// 4. output data
System.out.println(data);
ArffSaver arffSaverInstance = new ArffSaver();
arffSaverInstance.setInstances(data);
arffSaverInstance.setFile(new File("test.arff"));
arffSaverInstance.writeBatch();
}
在servlet类中:
DataForTrain testData = new DataForTrain();
try {
testData.createTestData(id_student, teacher_subject, id_level, isMandatory);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
解决方案是在Properties serch上找到Web部署程序集(右键单击项目=>属性)=> add =>从文件系统归档并在那里添加weka jar。