2014-09-06 43 views
0

我正在進行一種Java測試,其中用戶被給出這樣的問題:「4 +?= 12」。這些數字是隨機的,問題標記也是如此。Java。當userInput是一個字符串而不是int時如何產生錯誤信息?

我需要爲用戶輸入不是int時發出錯誤消息。例如,如果用戶鍵入單詞「八」而不是「8」,則會顯示錯誤消息。我怎樣才能做到這一點?

+3

您是否閱讀過[異常處理](http://docs.oracle.com/javase/tutorial/essential/exceptions/)? – PakkuDon 2014-09-06 12:23:33

+2

您可以嘗試將字符串響應轉換爲整數,並在轉換失敗的情況下捕獲異常(使用try ... catch塊 – ErstwhileIII 2014-09-06 12:23:41

+0

我們無法做任何事情,只能猜測沒有太多數據。你製作一個命令行程序? – bmargulies 2014-09-06 13:43:44

回答

2
Scanner scanner = new Scanner(System.in); 
    String input = scanner.nextLine();//get the next input line 
    scanner.close(); 
    Integer value = null; 
    try 
    { 
     value = Integer.valueOf(input); //if value cannot be parsed, a NumberFormatException will be thrown 
    } 
    catch (final NumberFormatException e) 
    { 
     System.out.println("Please enter an integer"); 
    } 


    if(value != null)//check if value has been parsed or not 
    { 
     //do something with the integer 

    } 
0

請考慮以下代碼,它們都確保提供了整數,並提示用戶輸入正確的輸入類型。你可以擴展的類來處理其他限制(最大/最小值等)

TestInput類

package com.example.input; 
import java.util.Scanner; 
public class TestInput { 
    public static void main(String[] args) { 
     int n; 
     Scanner myScanner = new Scanner(System.in); 

     n = Interact.getInt(myScanner,"Enter value for ?", "An integer is required"); 
     System.out.println("Resulting input = " + n); 
    } 
} 

互動類

package com.example.input; 
import java.util.Scanner; 
public class Interact { 

    public static int getInt(Scanner scanner, String promptMessage, 
      String errorMessage) { 
     int result = 0; 

     System.out.println(promptMessage); 
     boolean needInput = true; 
     while (needInput) { 
      String line = scanner.nextLine(); 
      try { 
       result = Integer.parseInt(line); 
       needInput = false; 
      } catch (Exception e) { 
       System.out.println(errorMessage); 
      } 
     } 
     return result; 
    } 
} 
0

首先,你需要獲取用戶爲'?'鍵入的字符串在問題中。如果你有System.inSystem.out做簡單的說,你會做這樣的:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
String line = in.readLine(); 

如果你想到一個簡單的版本界面,你可以使用JOptionPane這樣的:

String line = JOptionPane.showInputDialog("4 + ? = 12"); 

否則,如果你有一個真正的圖形用戶界面,你可以從一個JTextField或類似讀取用戶輸入:

String line = textField.getText(); 

(我在這種情況下,你也可以使用JFormattedTextField來過濾Integer的輸入。這將使檢查輸入是否是一個整數或沒有必要)


現在,你需要解析字符串的int

int value; 
try 
{ 
    value = Integer.parseInt(line); 
} 
catch (NumberFormatException nfe) 
{ 
    // the user didn't enter an Integer 
} 
doSomethingWith(value); 

catch - 塊的內容不同於你上面的變體。如果你帶着System.inSystem.out第一位的,你可以寫這樣的事情:

System.out.println("Your input is not an Integer! Please try again"); 

如果你拿了版本JOptionPane,你可以寫這樣的事情:

JOptionPane.showMessageDialog(null, "Your input is not an Integer! Please try again"); 

否則,你需要有一個JLabel爲那個女巫最初沒有內容。現在,將其設置爲3秒的文字:

errorLabel.setText("Your input is not an Integer! Please try again"); 
new Thread(() -> 
{ 
    try { Thread.sleep(3000); } // sleep 3 sec 
    catch (InterruptedException ie) {} // unable to sleep 3 sec 
    errorLabel.setText(""); 
}).start(); 

請注意,最後一個版本也與第二個版本兼容。

現在,您應該生成一個新問題,或重複讀/解析過程,直到用戶輸入一個整數。

+0

如果他使用Swing UI,他應該過濾任何不是數字的輸入,這就是格式化文本字段的確切目的=> http://docs.oracle.com/javase/ tutorial/uiswing/components/formattedtextfield.html。 – NoDataFound 2014-09-06 12:58:15

+0

@NoDataFound我認爲問題是如何檢查輸入是否是數字,而不是過濾它。否則,您也可以使用'JSpinner'或類似的東西 – msrd0 2014-09-06 12:59:39

+0

True。但是既然你提到了GUI,我可能會提出格式化的字段也會更好:) – NoDataFound 2014-09-06 13:02:49

相關問題