2013-12-18 5 views
0

안녕하세요, 스캐너에서 정수 값을 읽는 데 문제가 있습니다. 프로그램은 두 개의 배열을 정렬하고 정렬 된 값을 새 배열에 저장 한 다음 콘솔 출력에 씁니다.Java 스캐너 excenprion java.util.NoSuchElementException

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at U42Slevani.main(U42Slevani.java:40) 

여기 내 코드입니다 : 여기 는 예외입니다.

package u42slevani; 
import java.util.Scanner; 



/** 
* 
* @author matej.rehak 
*/ 
public class U42Slevani { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner sc = new Scanner(System.in); 
     int[] pole1; 
     int[] pole2; 
     int pocet=1; 


     while(pocet>0) 
     { 

      int pom; 
      int j; 



     pocet = sc.nextInt(); 
     if(pocet<0 || pocet>1000) 
     { 
      break; 
     } 
     pole1 = new int[pocet]; 
     for(int i = 0;i<pole1.length;i++) 
     { 
      pole1[i] = sc.nextInt(); 
     } 

     pocet = sc.nextInt(); //problem here 

     if(pocet<0 || pocet>1000) 
     { 
      break; 
     } 
     pole2 = new int[pocet]; 
     for(int i = 0;i<pole2.length;i++) 
     { 
      pole2[i] = sc.nextInt(); 
     } 

      for (int i = 1; i<pole1.length; i++) 
      { 
       pom = pole1[i]; 
       j = i - 1; 
       while ((j >= 0) && (pole1[j] > pom)) 
       { 
        pole1[j+1] = pole1[j]; 
        j--; 
       } 
       pole1[j+1] = pom; 
      } 

      for (int i = 1; i<pole2.length; i++) 
      { 
       pom = pole2[i]; 
       j = i - 1; 
       while ((j >= 0) && (pole2[j] > pom)) 
       { 
        pole2[j+1] = pole2[j]; 
        j--; 
       } 
       pole2[j+1] = pom; 
      } 

      int[] pole3 = new int[pole1.length+pole2.length]; 
      merge(pole3, pole1, pole2); 
      StringBuilder vystup = new StringBuilder(""); 
      for (int i = 0; i < pole3.length - 1; i++) 
      { 
       vystup.append(pole3[i] + " "); 
      } 
      vystup.append(pole3[pole3.length -1]); 
      System.out.println(vystup); 


     } 
    } 

    public static void merge(int[] list, int[] left, int[] right) { 
    int i = 0; 
    int j = 0; 
    // dokud nevyjedeme z jednoho z poli 
    while ((i < left.length) && (j < right.length)) { 
    // dosazeni toho mensiho prvku z obou poli a posunuti indexu 
    if (left[i] < right[j]) { 
     list[i + j] = left[i]; 
     i++; 
    } 
    else { 
     list[i + j] = right[j]; 
     j++; 
    } 
    } 
    // doliti zbytku z nevyprazdneneho pole 
    if (i < left.length) { 
    while (i < left.length) { 
     list[i + j] = left[i]; 
     i++; 
    } 
    } 
    else { 
    while (j < right.length) { 
     list[i + j] = right[j]; 
     j++; 
    } 
    } 
} 

} 

41 번 줄에 문제가 있다는 것을 알고 있지만 그 이유는 모르겠습니다. 고맙습니다.

+0

어느 라인 41입니까? 나는 셀 싶지 않아. – Math

+0

프로그램을 실행하고이 오류가 발생하면 무엇을 입력합니까? – reindeer

답변

0

Scanner의 기본 스트림에서 다음 항목을 읽으려고합니다. 읽을 항목이 있는지 확실하지 않지만 nextInt()을 호출하십시오. 읽을 정수가 없을 때 다음 정수를 읽으려고하면 NoSuchElementException이 발생합니다.

먼저 읽을 정수가 있는지 확인하고이 경우 읽습니다.

if(sc.hasNextInt()) 
{ 
    pocet = sc.nextInt(); 
}