我正在进行一种Java测试,其中用户被给出这样的问题:“4 +?= 12”。这些数字是随机的,问题标记也是如此。Java。当userInput是一个字符串而不是int时如何产生错误信息?
我需要为用户输入不是int时发出错误消息。例如,如果用户键入单词“八”而不是“8”,则会显示错误消息。我怎样才能做到这一点?
我正在进行一种Java测试,其中用户被给出这样的问题:“4 +?= 12”。这些数字是随机的,问题标记也是如此。Java。当userInput是一个字符串而不是int时如何产生错误信息?
我需要为用户输入不是int时发出错误消息。例如,如果用户键入单词“八”而不是“8”,则会显示错误消息。我怎样才能做到这一点?
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
}
请考虑以下代码,它们都确保提供了整数,并提示用户输入正确的输入类型。你可以扩展的类来处理其他限制(最大/最小值等)
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;
}
}
首先,你需要获取用户为'?'键入的字符串在问题中。如果你有System.in
和System.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.in
和System.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();
请注意,最后一个版本也与第二个版本兼容。
现在,您应该生成一个新问题,或重复读/解析过程,直到用户输入一个整数。
如果他使用Swing UI,他应该过滤任何不是数字的输入,这就是格式化文本字段的确切目的=> http://docs.oracle.com/javase/ tutorial/uiswing/components/formattedtextfield.html。 – NoDataFound 2014-09-06 12:58:15
@NoDataFound我认为问题是如何检查输入是否是数字,而不是过滤它。否则,您也可以使用'JSpinner'或类似的东西 – msrd0 2014-09-06 12:59:39
True。但是既然你提到了GUI,我可能会提出格式化的字段也会更好:) – NoDataFound 2014-09-06 13:02:49
您是否阅读过[异常处理](http://docs.oracle.com/javase/tutorial/essential/exceptions/)? – PakkuDon 2014-09-06 12:23:33
您可以尝试将字符串响应转换为整数,并在转换失败的情况下捕获异常(使用try ... catch块 – ErstwhileIII 2014-09-06 12:23:41
我们无法做任何事情,只能猜测没有太多数据。你制作一个命令行程序? – bmargulies 2014-09-06 13:43:44