简体   繁体   English

如何使用JOptionPane捕获不正确的输入?

[英]How to catch incorrect input using JOptionPane?

Hey I have to ask the user for a purchase amount using JOptionPane and if they input more than two decimal places, nothing, characters, or more than one decimal point the program has to show an error message and stop. 嘿,我必须使用JOptionPane向用户询问购买金额,并且如果他们输入了两个以上的小数位,什么都没有,字符或一个以上的小数点,则程序必须显示错误消息并停止。

How would I do this? 我该怎么做?

I don't want someone to write the program for me just a link that explains how I would do it 我不希望有人为我编写程序,只是一个链接,该链接说明了我的操作方式

if the user inputs "12.526" or " " or "1.3.25" or "abc" I want the program to show an error message and stop. 如果用户输入“ 12.526”或“”或“ 1.3.25”或“ abc”,我希望程序显示错误消息并停止。

Since this appears to be a confusing question or I'm asking it incorrectly these are my teachers directions exactly: 由于这似乎是一个令人困惑的问题,或者我问的不正确,所以这些正是我的老师的指导:

  1. The program must ask the user to input the purchase amount, using JOptionPane.showInputDialog 该程序必须要求用户使用JOptionPane.showInputDialog输入购买金额。
  2. The program must ask the user to input the payment amount, using JOptionPane.showInputDialog 该程序必须要求用户使用JOptionPane.showInputDialog输入付款金额。
  3. The program must catch incorrect input (empty, null, characters, more than one decimal point, more than two decimal places (ie 3.567)) display an error message and stop the program execution. 程序必须捕获不正确的输入(空,空,字符,多于一个小数点,多于两个小数位(即3.567)),以显示错误消息并停止程序执行。
  String PurchaseAmount = JOptionPane.showInputDialog(null, String.format("What is your purchase amount?")); 
  double PurchaseTotal = Double.parseDouble(PurchaseAmount);
  String PaymentAmount = JOptionPane.showInputDialog(null, "What is the payment amount?");
  double PaymentGiven = Double.parseDouble(PaymentAmount);

The first part of checking whether the input is of the right format is as you've done: use Double.parseDouble() to convert the String to a double . 检查输入是否格式正确的第一部分就是您已经完成的工作:使用Double.parseDouble()String转换为double When you do this, you can use a try / catch block to catch a NumberFormatException , which will occur if the input couldn't be turned into a floating point number at all. 执行此操作时,可以使用try / catch块来捕获NumberFormatException ,如果根本无法将输入转换为浮点数,则会发生该异常。

But you've also been asked to check that you don't have more than two digits after the decimal point. 但是,还要求您检查小数点后的位数是否超过两位。 This is a little trickier. 这有点棘手。 I would suggest that you use String.indexOf() to find the decimal point in the input string, and then check that there aren't more than two characters (by which I include digits) after that point. 我建议您使用String.indexOf()查找输入字符串中的小数点,然后检查在该点之后最多两个字符(包括数字)。

There are other options, but they are probably trickier if you're not familiar with them: 还有其他选项,但是如果您不熟悉它们,它们可能会更棘手:

  1. Use a regular expression. 使用正则表达式。
  2. Convert to a BigDecimal , and then check that if you round to two decimal places, you end up with the same BigDecimal that you started with. 转换为BigDecimal ,然后检查是否四舍五入到小数点后两位是否与开始时使用的BigDecimal相同。

Note: do not try this second approach with a double . 注意: 不要试图用第二种方法double A double can't always represent decimal numbers exactly, so rounding to two decimal places and checking it's unchanged might give you false positives (seeming errors where there aren't errors). double精度不能总是精确地表示十进制数字,因此舍入到小数点后两位并检查其是否不变可能会给您带来误报(在没有错误的地方出现错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM