2011-06-13 57 views
0

我有這個小問題,因爲它產生java.util.NoSuchElementException這個對象數組到一個向量,因爲它產生java.util.NoSuchElementException我找不到什麼似乎是問題。任何人都可以點似乎是個錯誤,請這裏是代碼,分裂整數數組

import java.util.Collections; 
import java.util.Vector; 


public class Splitting { 

    /** 
    * @param args 
    */ 

    protected int [] temp; 
    Vector<Integer> vec = new Vector<Integer>(); 

    public void split(String input) 
    { 
     if (input == null) 
     { 
     String[] str; 
     str = input.split(","); 
     temp = new int[str.length]; 

     System.out.println(str); 

      for (int i = 0; i < str.length; i++) 
      { 

       temp[i] = Integer.parseInt(str[i]); 
       vec.add(temp[i]); 

      } 
     } 
     System.out.println(vec); 
     Collections.sort(vec); 


     System.out.println(vec); 
     Collections.max(vec); 
    } 



    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Splitting obj = new Splitting(); 

     obj.split("12,65,21,23,89,67,12"); 



    } 

} 
+2

首先,異常通常有一個行號和堆棧跟蹤。你應該向我們展示這些,他們幫助。另外,我想你想'if(input!= null)'。 – trutheality 2011-06-13 03:05:36

+0

@Matt Ball:'Vector'是一個'List'。 – trutheality 2011-06-13 03:07:30

+0

@trutheality:謝謝,我輸入得太快了。在幾乎所有情況下,應該使用'java.util.ArrayList'來代替'Vector'。 'Vector'是一個遺留類型(來自JDK 1.0)並且是'synchronized'。 – 2011-06-13 03:49:40

回答

2

if (input == null) 

您的意思是有

if (input != null) 

+0

請參閱http://ideone.com/0wVoZ – 2011-06-13 03:05:47

2

可能會改爲if (input == null)應該是if (input != null)

3

可能應該是

if (input != null) 

可以數組轉換爲矢量簡單,接着片斷:

vec = new Vector(Arrays.asList(str)); 

也許,它不會工作你的情況(因爲你需要解析字符串到整數),但在未來很好知道。 感謝

+0

非常感謝您的指示。它現在有效 – Splitter 2011-06-13 03:09:45

+3

你應該接受答案,如果它是正確的,對不對? – 2011-06-13 03:11:42

0

使用Guava

所有的
String input = "Some very stupid data with ids of invoces like 121432, 3436534 and 8989898 inside"; 
List<String> l =Lists.newArrayList(Splitter.on(" ").split(input)); 
Collection<Integer> c = Collections2.transform(l, new Function<String, Integer>(){ 
    @Override 
    public Integer apply(String s) { 
     return Integer.parseInt(s); 
    }}); 
List<Integer> l2 = Lists.newArrayList(c); 

Collections.sort(l2); 
Collections.max(l2);