- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我无法让我的程序响应空输入。例如,假设我想提示用户输入类型为 BigDecimal
的货币值(value)以及货币类型。这是有问题的程序的样子。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of money "
+ "and specify currency (USD or CNY): ");
// initialize variable moneyInput
BigDecimal moneyInput;
// check if moneyInput is numeric
try {
moneyInput = input.nextBigDecimal();
} catch (InputMismatchException e){
System.err.println("InputMismatchException: non-numeric value");
return;
}
// specify currency of moneyInput
String currencyType = input.next();
...
}
所以在这里,像 100.00 USD
这样的输入可以正常工作。
Enter the amount of money and specify currency (USD or CNY): 100.00 USD
$100.00 USD ≈ ¥670.17 CNY
像 ASDF USD
这样的输入会适本地导致错误消息。
Enter the amount of money and specify currency (USD or CNY): ASDF USD
InputMismatchException: non-numeric value
但是,我如何强制程序响应空白输入,要么立即按下回车键,要么在第一行输入一堆空格?例如:
Enter the amount of money and specify currency (USD or CNY):
1000.00 USD
$1000.00 USD ≈ ¥6701.70 CNY
在上面的示例中,用户可以无限期地按下返回键,直到输入可读的内容(有效或无效)。 我想实现某种方法来检查用户是否在没有输入任何有意义的内容的情况下按下了返回键。
另一个我不知道如何解释的结果是,如果用户只输入了货币值,然后在最终输入货币之前多次按 [return]。
例子:
Enter the amount of money and specify currency (USD or CNY): 100.00
USD
$100.00 USD ≈ ¥670.17 CNY
如何让程序在输入数值并按下 [return] 后提示用户类似 Please specify the currency:
的内容?有点像
Enter the amount of money and specify currency (USD or CNY): 100.00
Please specify the currency: USD
$100.00 USD ≈ ¥670.17 CNY
我能够通过将 try-catch block 之后的部分更改为以下部分来实现上述功能:
input.nextLine();
System.out.print("Please specify the currency: ");
String currencyType = input.nextLine();
但是使用这种方法,程序失去了允许用户在一行中输入 moneyInput
和 currencyType
的能力,就像第一个例子中那样。 (同样,仍然存在能够为每个提示无限期地按下 [return] 直到最终输入可读内容的问题)。
感谢阅读,抱歉文章太长。
最佳答案
Scanner
提供了两个基本的——有时是相互冲突的——用例:
一般来说,将两者混合是一个糟糕的主意(它有效,但可能不是你想要的)。 next()
和 nextBigDecimal()
等 token 读取方法会忽略换行符。
如果你想处理 Enter 你需要使用 Scanner.nextLine()
逐行读取用户的输入并单独解析每一行(即 line.split("\\s+")
),而不是使用 Scanner
的 token 读取方法。
有些人喜欢使用嵌套的 Scanner
并用一个 Scanner
逐行读取输入,然后将行传递到一个新的 Scanner
以只标记那一行。
例如:
try (Scanner in = new Scanner(System.in)) {
while (in.hasNextLine()) {
try {
String line = in.nextLine();
Scanner lineScan = new Scanner(line);
BigDecimal moneyInput = lineScan.nextBigDecimal();
String currency = lineScan.next();
// do something
} catch (NoSuchElementException | IllegalStateException e) {
System.err.print("Please enter the VALUE followed by the CURRENCY");
}
}
}
如果您不想使用嵌套的 Scanner
,还有许多其他大致等效的机制。这是要点,但您可能希望添加额外的错误处理代码(例如,如果 new BigDecimal()
抛出异常:
使用 String.split()
:
String[] parts = line.split("\\s+");
if (parts.length == 2) {
BigDecimal moneyInput = new BigDecimal(parts[0]);
String currency = parts[1];
// do something
} else {
System.err.println("Please enter the VALUE followed by the CURRENCY");
}
使用模式
:
/**
* Matches one or more digits, optionally followed by a . and more digits,
* followed by whitespace then one or more uppercase letters.
*/
private static final Pattern MONEY_PATTERN =
Pattern.compile("(\\d+(?:\\.\\d+))\\s+([A-Z]+)");
然后:
Matcher m = MONEY_PATTERN.matcher(line);
if (m.matches()) {
BigDecimal moneyInput = new BigDecimal(m.group(1));
String currency = m.group(2);
// do something
} else {
System.err.println("Please enter the VALUE followed by the CURRENCY");
}
关于java - 如何让 Java 扫描器确认空白输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471220/
TCP header 上的 32 位确认字段,比如 x告诉另一台主机“我收到了所有字节,直到并包括 x-1,现在期待来自 x 和 on 的字节”。在这种情况下,接收方可能已经收到了一些更多字节,比如
我正在使用 PyCharm 2020.2.3 不知不觉中我点击了下图中的复选框 现在,即使我的代码正在调试中,点击运行也会终止调试并开始运行代码。如何将其恢复为未选中状态?谢谢。 PS:我的“允许并行
我想知道何时使用 RabbitMQ 和 Spring Boot 接受 (ack) 或不接受 (nack) 消息。 我想将消息发送到队列(通过交换)并检查队列是否已接受该消息。实际上我想发送到两个不同的
我一直在寻找一种方法让用户确认 票在分配给他们之后。不知道有没有 这是一个内置功能,或者如果有一个插件 将为用户创建一个状态/按钮以接受票证 在它被放入队列之后。我希望 从附近的售票窗口看到类似的东西
我正在构建一个应用程序以通过 Xbee API 与 Xbee 模块通信。 目前我有一些工作,但它相当简单并且有很多限制。 Sub processPackets() ' this runs as its
我有一个复选框,更改后会自动发布。 自动发布对于两者(选中和未选中)都适用,但我想在每个事件发生之前弹出一个对话框进行确认。 到目前为止,当选中该复选框时,弹出框就会起作用。 但当取消选中该复选框时,
当我使用 UIGestureRecognizer ,例如,当用户向右滑动时,我想要一个 UIAlertView询问他是否真的要进行向右滑动的 Action 。 我曾尝试这样做,但没有成功。 最佳答案
我有一个 asp:CheckBoxList,我想显示一条警告消息仅在使用 jquery 取消选中复选框时。 $('.chklist').click( function () {
我想知道有什么可能的方法来确定我们的推送消息是否最终从 APNS 服务器传送。我已经想出了一些信息,如下所述 APNS 正在发送接受推送请求的响应代码,并可能给出错误代码(如果有)。例如:如果您的有效
我有此页面,我正在尝试确认输入文本字段中的日期与当前日期。如果输入字段中的日期晚于当前日期,则需要出现确认框以确认他们输入了正确的日期。因此,“确定”按钮需要完成数据提交,“取消”按钮需要将它们返回到
我有一个功能: function placeOrder(price, productList) { var bulletinBoardItem = Number(productList.box
我不明白为什么即使我点击“否”,这个confirm()调用也会被触发。你能告诉我我做错了什么吗? $('.deleteButton').livequery('click',function(){
我目前正在使用 dotmailer 生成一个新表单(简单的文本框和提交按钮),自动将电子邮件地址添加到 dotmailer 地址簿。 当有人提交电子邮件地址时 - 他们可以被带到网页。 我一直在尝试
这是不起作用的代码...它只是删除表单而不先提示。 $(".delete").click(function () { if(confirm('You honestly want to dele
我在我的程序中使用 aprgeparse 创建一个参数,允许用户从 amazon s3 存储桶中删除文件。我以这种方式创建它: parser.add_argument("-r", "--remove"
我正在努力学习 puppeteer 操作。我已经成功编写了登录页面和一些导航的脚本。然后我让它点击一个按钮。该页面抛出一个 window.confirm,我希望我的脚本接受它以继续下一步,但我不知
某网站实现了一个第三方插件,提示用户在删除前进行确认。 confirmDelete: function (event) { var go_ahead = confirm("Are you su
我想在 primefaces 的选择/取消选择复选框上显示确认对话框。我试过了 但它不起作用,因为 selectBooleanCheckBox 不可确认。是否有解决此问题的解决方法? 最
我们已经从 TFS 下载了一个项目,在恢复 Nuget 包后,我们收到以下错误: Error 5 The "ValidatePackageReferences" task could not
我有两个单独的 ul 列表:列表 A 和列表 B 由于 jQuery UI 插件,它们都可以排序。 我正在开发的项目的用户希望在项目从一个列表移动到另一个列表时确认操作,但在同一列表内移动时则不需要。
我是一名优秀的程序员,十分优秀!