2014-04-30 3 views
2

사용자가 1과 10 사이의 숫자를 입력 한 다음 숫자를 표시하도록 요청하는 간단한 프로그램을 작성하려고합니다. 이제 숫자가 범위를 벗어나면 다시 질문 할 수 있지만 숫자가 아닌 다른 것이 입력되면 다시 묻을 수는 없습니다 (예 : % 또는 hello).JOptionPane 입력을 정수로 변환

소스 코드 : (상단을 잘라)

public static void main(String[] args){ 

    int number;     //For holding the number 
    String stringInput;   //For holding the string values until converted 


    //------------------// 
    //Introducing the user 

    JOptionPane.showMessageDialog(null, "This is a program that will ask \n" 
             + "you to enter a number in-between \n" 
             + "1-10, if the number is not within \n" 
             + "the parameters, the program will repeat."); 


    //---------------------// 
    //Get input from the user 

    stringInput = JOptionPane.showInputDialog("Enter number."); 

    number = Integer.parseInt(stringInput); 


    //-----------------// 
    //Checking the number 

    while (number > 10 || number < 0){ 

     stringInput = JOptionPane.showInputDialog("That number is not within the \n" 
                + "allowed range! Enter another number."); 

     number = Integer.parseInt(stringInput); 

    } 


    //-------------------// 
    //Displaying the number 

    JOptionPane.showMessageDialog(null, "The number you chose is " 
             + number 
             + "."); 


    //-------------// 
    //Closing it down 
     System.exit(0); 
} 

가장 큰 문제가 있습니다 : 내가 제대로 데이터 값을 변환 할 수없는 것

number = Integer.parseInt(stringInput); 

. 이미 if 문을 사용하여 숫자가 정수인지 확인하는 것과 같은 것을 생각했지만 확인 방법을 알 수 없었습니다. 내가 할 수 있으면 좋겠다 :

if (number == Integer) 

나는 아직도 자바에 매우 익숙하지 않다. 어떤 도움을 주셔서 감사합니다. 시간을내어 읽어 주셔서 감사합니다.

답변

2

당신은 같은 잘못된 입력을 감지하는 try/catch 블록으로 Integer.parseInt()에 전화를 둘러싸해야합니다

String errorMessage = ""; 
do { 
    // Show input dialog with current error message, if any 
    String stringInput = JOptionPane.showInputDialog(errorMessage + "Enter number."); 
    try { 
     int number = Integer.parseInt(stringInput); 
     if (number > 10 || number < 0) { 
      errorMessage = "That number is not within the \n" + "allowed range!\n"; 
     } else { 
      JOptionPane 
       .showMessageDialog(null, "The number you chose is " + number + "."); 
      errorMessage = ""; // no more error 
     } 
    } catch (NumberFormatException e) { 
     // The typed text was not an integer 
     errorMessage = "The text you typed is not a number.\n"; 
    } 
} while (!errorMessage.isEmpty()); 
1

내가 노력하고있어 여기에

try { 
    number = Integer.parseInt(stringInput); 
} catch (NumberFormatException e) { 
    // Not a number, display error message... 
} 

는 솔루션입니다 사용자에게 요청할 간단한 프로그램을 작성하십시오. 1과 10 사이의 숫자를 입력하십시오.

문자열의 정수를 구문 분석하지 않고, 매우 짧고 간단해야한다.

import java.awt.EventQueue; 
import javax.swing.Icon; 
import javax.swing.JOptionPane; 
import javax.swing.UIManager; 

public class MyOptionPane { 

    public MyOptionPane() { 
     Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); 
     Object[] possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
     Integer i = (Integer) JOptionPane.showOptionDialog(null, 
       null, "ShowInputDialog", 
       JOptionPane.PLAIN_MESSAGE, 1, errorIcon, possibilities, 0); 

     // or 

     Integer ii = (Integer) JOptionPane.showInputDialog(null, 
       "Select number:\n\from JComboBox", "ShowInputDialog", 
       JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers"); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       MyOptionPane mOP = new MyOptionPane(); 
      } 
     }); 
    } 
}