2012-05-03 108 views
-1

我正在写一个包装,需要从weka库中导入一些函数;然而,它扔给我以下错误:java未报告异常java.lang.Exception错误编译代码合并weka时

未报告的异常java.lang.Exception;必须捕获或声明抛出

我的代码如下:

import java.io.*; 
import weka.core.Instances; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import weka.core.converters.ConverterUtils.DataSource; 
import java.lang.Integer; 

public class wrapper 
{ 
public static void main(String[] args) 
{ 

    try 
    { 
     Runtime r = Runtime.getRuntime(); 
     Process p = r.exec("python frequency_counter_two.py nono 400 0"); 
     BufferedReader br = new BufferedReader(new   
     InputStreamReader(p.getInputStream())); 
     p.waitFor(); 
     String line = ""; 
     while (br.ready()) 
      System.out.println(br.readLine()); 

    } 
    catch (Exception e) 
    { 
    String cause = e.getMessage(); 
    if (cause.equals("python: not found")) 
     System.out.println("No python interpreter found."); 
    } 

run_weka(); 


} 



public static int run_weka() 
{ 

DataSource source = new DataSource("features_ten_topics_10_unigrams_0_bigrams.csv"); 
Instances data = source.getDataSet(); 
// setting class attribute if the data format does not provide this information 
// For example, the XRFF format saves the class attribute information as well 
if (data.classIndex() == -1) 
    data.setClassIndex(data.numAttributes() - 1); 

/* 

double percent = 66.0; 
Instances inst = data; // your full training set 
instances.randomize(java.util.Random); 
int trainSize = (int) Math.round(inst.numInstances() * percent/100); 
int testSize = inst.numInstances() - trainSize; 
Instances train = new Instances(inst, 0, trainSize); 
Instances test = new Instances(inst, trainSize, testSize); 

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

*/ 
} 

} 

什么可能会在任何想法?

+0

你不使用IDE?虽然我不知道weka,但我很确定您的“run_weka”方法必须重新抛出或捕获一些异常... – home

+0

如果您的问题已得到解答,或者它不再有效,请勾选以选择最合适的答案让大家都知道问题已经解决。谢谢。 – wattostudios

回答

0

基本上它是说一些秧鸡方法可以抛出一个异常,所以你需要编写一些代码来处理这个事件。

在这种情况下,你可能会改变你的方法做这样的事情...

public static int run_weka() { 
    Instances data; 

    try { 
     DataSource source = new DataSource("features_ten_topics_10_unigrams_0_bigrams.csv"); 
     data = source.getDataSet(); 
    } 
    catch (Exception e){ 
     System.out.println("An error occurred: " + e); 
     return -1; 
    } 

    // setting class attribute if the data format does not provide this information 
    // For example, the XRFF format saves the class attribute information as well 
    if (data.classIndex() == -1) 
     data.setClassIndex(data.numAttributes() - 1); 
    /* 
    Your commented code... 
    */ 
    } 
}