2013-08-23 55 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

相關問題