2011-06-13 2 views
0

이 배열을 벡터에 생성 할 때이 작은 문제가 발생하여 java.util.NoSuchElementException이 발생합니다. 문제의 원인을 찾을 수 없습니다. 오류 코드는 여기에 만족 것으로 보인다 누구 포인트, 당신은정수 배열 나누기

if (input == null) 

당신이

if (input != null) 

을 가지고하셨습니까 한

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

+0

@Matt Ball :'Vector'는'List'입니다. – trutheality

+0

@trutheality : 감사합니다. 너무 빨리 입력했습니다. 거의 모든 경우에'java.util.ArrayList'가'Vector' 대신에 사용되어야합니다. 'Vector'는 JDK 1.0의 레거시 타입이며'synchronized'입니다. –

답변

2

수 있습니까?

+0

참조 http://ideone.com/0wVoZ –

2

대신 if (input == null)이 (가) if (input != null)이되어야합니까?

3

if (input != null) 

당신은 단순히 다음 조각과 벡터에 배열을 변환 할 수 있습니다해야 할 수 있습니다 : 당신이 문자열을 구문 분석해야하기 때문에 (

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

아마, 그것은 귀하의 경우에는 작동하지 않을 것입니다 정수로) 그러나 미래에는 알고있는 것이 좋다. 감사

+0

감사합니다. 지금 작동합니다 – Splitter

+3

맞으면 대답을 받아 들여야합니다. 맞습니까? –

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);