java 当使用hasNextDouble()验证价格输入时,它会打印“Invalid Input”,然后才要求输入[关闭]

rryofs0p  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(165)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

2天前关闭。
Improve this question
所以我有一个java程序,它要求用户将产品添加到系统中。当验证产品的价格时,我收到的输出是:

  1. Enter Product Price:
  2. Enter Number of Available Items:

字符串
它似乎每次都跳过产品价格字段,转到“可用产品数量:”字段。
这是我对输入进行验证的实现。
编辑:

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. // double productPrice = 0.0;
  6. System.out.print("\nEnter Product Name: ");
  7. String productName = input.next();
  8. // System.out.println();
  9. System.out.print("\nEnter Product Price: ");
  10. // Check if the price input is a valid double/int
  11. while (!input.hasNextDouble()) {
  12. // System.out.flush();
  13. System.out.print("\nInvalid input");
  14. input.nextLine();
  15. System.out.print("\nEnter Product Price: ");
  16. }
  17. double productPrice = input.nextDouble();
  18. System.out.print("\nEnter Number of Available Items: ");
  19. int noOfAvailableItems = input.nextInt();
  20. }
  21. }


在对不同的输入进行了几次测试之后,我发现了一些令人困惑的行为。
当我输入一个没有空格的值作为产品名称时,价格字段显示为要求输入预期的方式。

  1. Enter Product Name: iphone13
  2. Enter Product Price: 99.00


然而,在这方面,
对于像iPhone 13这样带有空格的输入,它完全跳过了产品价格字段。我不知道这可能是什么原因。

  1. Enter Product Name: iphone 15
  2. Enter Product Price:
  3. Enter Number of Available Items:


上面只是我代码中的一小部分,因为这是为了一个任务而做的。
我已经尝试了几种不同的验证方法,但似乎没有一种可以解决这个问题。

  1. do {
  2. input.nextLine();
  3. System.out.println("Invalid input");
  4. System.out.print("\nEnter Product Price: ");
  5. } while (!input.hasNextDouble());
  6. double productPrice = input.nextDouble();


这是我尝试的另一种方法,但这次我的输出是:

  1. Enter Product Price: Invalid input
  2. Enter Product Price:


编辑:如前所述,添加了一个最小可重现示例。

s3fp2yjn

s3fp2yjn1#

如果你坚持使用Scanner#next()而不是Scanner#nextLine(),那么你可以这样声明你的Scanner:

  1. Scanner input = new Scanner(System.in).useDelimiter("\\R");

字符串
由于productName中可能有空格,.useDelimiter("\\R")将使next()将整个条目带到换行符。next() * 通常 * 用于检索空格分隔的字符串标记(但不总是)。我个人更喜欢使用Scanner#nextLine()用于所有控制台提示。
不需要多次输出提示符。将提示符放在while循环中就足够了:

  1. Scanner input = new Scanner(System.in).useDelimiter("\\R");
  2. // Get price of product
  3. String priceStrg = "";
  4. while (priceStrg.isEmpty()) {
  5. System.out.print("\nEnter Product Price: -> ");
  6. priceStrg = input.next().trim();
  7. /* If you use `nextLine()` above instead of `next()` then
  8. you can get rid of `.useDelimiter("\\R")`. */
  9. /* Validates that a string representation of a signed
  10. or unsigned Integer or floating point value was
  11. actually supplied. */
  12. if (!priceStrg.matches("-?\\d+(\\.\\d+)?")) {
  13. System.out.println("Invalid Entry! (" + priceStrg
  14. + ") - Try again..." + System.lineSeparator());
  15. priceStrg = ""; // Empty prompt input variable to ensure re-loop.
  16. }
  17. }
  18. /* If we make it to this point then entry was valid!
  19. Convert `priceStrg` to a double type and store
  20. within the `productPrice` double type variable: */
  21. double productPrice = Double.parseDouble(priceStrg);


带有Regular ExpressionString#matches()方法用于验证User条目。表达式的作用是确保User提供的条目字符串只不过是一个有符号或无符号整数或浮点值的字符串表示。

展开查看全部
busg9geu

busg9geu2#

这个问题围绕着使用正确的方法接受输入。
使用Scanner类的nextLine()方法从用户那里获取一个字符串。nextLine()方法读取文本直到行尾。阅读行尾后,它将光标扔到下一行。

  1. System.out.print("\nEnter Product Name: ");
  2. String productName = input.nextLine();

字符串

相关问题