2012-10-27 3 views
2

현재 소수를 테스트하는 작은 앱을 작성 중입니다. GUI 기반이지만 한 가지 문제가 있습니다. 사용자가 numberformatexception 핸들러를 사용하여 숫자 만 입력 할 수있는 제약 조건을 추가했지만 사용자가 9 자리 이상의 숫자를 입력 할 때마다 더 이상 숫자를 고려하지 않습니다. 이 문제에 대한 해결책이 있습니까? 아래에 제 코드를 남겼습니다. 클래스 Algorithim 가정내 프로그램에서 9 자리 이상의 숫자를 허용하려면 어떻게해야합니까?

static void validation() // This is what happens when the "Check" button is clicked 
{ 


    // Retrieve information from the fields and print it out on the Frame 
    if (jtfX.getText().trim().length() == 0) // Check if the field is empty 
    { 
     jlSolution.setText("You have not entered anything yet"); 

    } 

    else // Otherwise... 
    { 

     try // In general.... 
     { 

      if (Long.parseLong(jtfX.getText()) < 0) // Check if it is a negative value 
      { 
           jlSolution.setText("The number you entered is less than zero"); 
      } 
          else // If it isn't... 
      { 
           jlSolution.setText(new Algorithm(Integer.parseInt(jtfX.getText())).check()); // ....then check if this number is prime. 
      } 
     } 

     catch (NumberFormatException nfe) // ... always catch those who refuse to follow simple rules! 
     { 
      jlSolution.setText("Numerical values only please. " + "You entered: " + jtfX.getText()); 

     } 
    } 

} 
+0

더 코드 PLZ,'체크()'? 'ALGORITHM()'9 자리 이상에 대해, BigInteger를 사용해야합니다. –

+0

'long'만으로도 충분하다면,'BigInteger'를 사용하는 것보다 훨씬 작습니다. –

+0

감사합니다. Daniel –

답변

1

더 큰 값을 유지하기 위해 BigInteger과의 생성자에서 정수의 인수를 대체 할 수있는 사용자 정의 된 클래스이다.

당신은 너무처럼 jlSolution 필드를 업데이트합니다 :

Algorithm algorithm = new Algorithm(new BigInteger(jtfX.getText())); 
jlSolution.setText(algorithm.check()); 
+0

감사합니다. 정말 고맙습니다. –

관련 문제