2013-08-23 23 views
0

我是java开发领域的初学者,仍然是Java编程的学习者。我想在NetBeans IDE上看到支持向量机分类器的输出。所以我复制了这个附加的代码片段,并尝试使用所有其他所需的类和主要方法运行,但我得到Number format exception当我给一个文件包含输入如23,25,26,27在方法调用期间loadBinaryProblem()在主要方法,如果我删除所有的逗号,并将其替换为空间例如:23 25 26 27然后我得到ArrayIndexOutOfBound异常,而不是它。所以任何人都可以帮助正确地获得输出而不会出现任何错误。使用ArrayList时解决ArrayIndexOutOfBoundException

package svmlearn; 

import java.io.*; 
import java.util.*; 

/** 
* Class representing an optimization problem (a data setting); 
* taken from liblinear; "bias" excluded 
* @author miafranc 
* 
*/ 
public class Problem { 
     /** The number of training data */ 
     public int l; 
     /** The number of features (including the bias feature if bias >= 0) */ 
     public int n; 
     /** Array containing the target values */ 
     public int[] y; 
     /** Map of categories to allow various ID's to identify classes with. */ 
     public CategoryMap<Integer> catmap; 
     /** Array of sparse feature nodes */ 
     public FeatureNode[][] x; 
     public Problem() { 
       l = 0; 
       n = 0; 
       catmap = new CategoryMap<Integer>(); 
     } 
     /** 
     * Loads a binary problem from file, i.e. having 2 classes. 
     * @param filename The filename containing the problem in LibSVM format. 
     */ 
     public void loadBinaryProblem(String filename) { 
       String row; 
       ArrayList<Integer> classes = new ArrayList<Integer>(); 
       ArrayList<FeatureNode []> examples = new ArrayList<FeatureNode []>(); 
       try { 
         BufferedReader r = new BufferedReader(new FileReader(filename)); 
         while ((row = r.readLine()) != null) { 
           String [] elems = row.split(" "); 
           //Category: 
           Integer cat = Integer.parseInt(elems[0]); 
           catmap.addCategory(cat); 
           if (catmap.size() > 2) { 
             throw new IllegalArgumentException("only 2 classes allowed!"); 
           } 
           classes.add(catmap.getNewCategoryOf(cat)); 
           //Index/value pairs: 
           examples.add(parseRow(elems)); 
         } 
         x = new FeatureNode[examples.size()][]; 
         y = new int[examples.size()]; 
         for (int i=0; i<examples.size(); i++) { 
           x[i] = examples.get(i); 
           y[i] = 2*classes.get(i)-1; //0,1 => -1,1 
         } 
         l = examples.size(); 
       } catch (Exception e) { 
         System.out.println(e); 
       } 
     } 
     /** 
     * Parses a row from a LibSVM format file. 
     * @param row The already split row on spaces. 
     * @return The corresponding FeatureNode. 
     */ 
     public FeatureNode [] parseRow(String [] row) { 
       FeatureNode [] example = new FeatureNode[row.length-1]; 
       int maxindex = 0; 
       for (int i=1; i<row.length; i++) { 
         String [] iv = row[i].split(":"); 
         int index = Integer.parseInt(iv[0]); 
         if (index <= maxindex) { 
           throw new IllegalArgumentException("indices must be in increasing order!"); 
         } 
         maxindex = index; 
         double value = Double.parseDouble(iv[1]); 
         example[i-1] = new FeatureNode(index, value); 
       } 
       if (n < maxindex) 
         n = maxindex; 
       return example; 
     } 
} 
+0

嗨,你有没有尝试过使用断点调试?这将有助于特别是在处理这类问题时。你有一个冗长的代码。 –

+0

看到'split'方法,我假设你的输入必须是由空格和冒号分隔的数字组合。哪条线投掷错误? – npinti

+0

嗨,谢谢你的回应。实际上,我将.csv文件作为函数的输入。这个文件包含逗号分隔的变量,就像23,25,26等......这里没有空格或冒号。我最初试图用文件组成完整整数 – Priya

回答

4

我猜NumberformatExceptions来自:

String [] elems = row.split(" "); //nothing done by "23,25,26,27" 
//Category: 
Integer cat = Integer.parseInt(elems[0]); //you are trying to parse "23,25,26,27" 

ArrayIndexOutOfBound来自:

String [] iv = row[i].split(":");//nothing done 
... 
double value = Double.parseDouble(iv[1]);//1 is out of bound 
+0

谢谢你的回应。我得到的错误为java.lang.NumberFormatException:对于上述附加程序的输入字符串:“23,25,26,27”。但是如果我改变语句String [] elems = row.split(“”); into String [] elems = row.split(“,”);我得到的错误为java.lang.ArrayIndexOutOfBoundsException:1请帮我在哪里修改代码。 – Priya

+0

如果您更改为split(“,”),您仍会收到第二个错误。你尝试拆分(“:”)ans,因为你的字符串中没有任何“:”,你会得到这个错误。尝试另一个输入字符串:“12 32:2.4 33:2.5”这应该运行没有错误。 12将是类别,32-> 2.4索引 - >值,33-> 2.5索引 - >值 – JohnnyAW

+0

如果我正在尝试根据您的建议我得到错误如下加载。 异常线程 “main” java.lang.ArrayIndexOutOfBoundsException:1个 培训... * \t在svmlearn.Completesvm.simpleSMO(Completesvm.java:98) \t在svmlearn.Completesvm.svmTrain(Completesvm.java:60) \t在svmlearn.Completesvm.svmTrain(Completesvm.java:49) \t在svmlearn.Completesvm.main(Completesvm.java:303) Java结果:1 生成成功(总时间:0秒) – Priya

相关问题