【问题标题】:Scanner: scan int or string扫描仪:扫描整数或字符串
【发布时间】:2018-01-02 18:32:25
【问题描述】:

尝试使用 Scanner 获取 int 或 string.. 下面的代码是我所拥有的:

System.out.println("where would you like to send the following item: ");
System.out.println("1. groceries, 2. savings, 3. utilities");
System.out.print("Enter list name or list number to send: ");
String user = user_input.nextLine();
      if (user.toLowerCase().equals("groceries") || Integer.parseInt(user) == 1) {
             System.out.println("item was sent to the grocery list");

      }else if(user.toLowerCase().equals("savings") || Integer.parseInt(user) == 2) {
             System.out.println("item was sent to the savings list");

      }else if(user.toLowerCase().contains("utilities") || Integer.parseInt(user) == 3) {
             System.out.println("item was sent to the utilities list");

      }else{
             System.out.println("item is not in any category");

      }

我的问题是,每当我输入字符串时,都会收到 NumberFormatException 错误。键入 int 不会给我带来任何问题。我认为使用 String user = user_input.nextLine(); 获取输入并使用 Integer.parseInt(user) 在 if 语句中将数字字符串转换为 int 可以解决问题,但它仍然给我错误消息。

非常感谢任何帮助...

【问题讨论】:

  • parseInt 如果输入字符串不代表整数,则抛出 NumberFormatException。见docs
  • 您可以避免使用parseIntuser.equals("1") 等。
  • 让我澄清一下:if (user.toLowerCase().equals("groceries") || user.equals("1")) {
  • 我很抱歉@JohnnyMopp ...我应该知道这就是您的意思...实际上确实有效...谢谢。你帮我解决了我的问题

标签: java string if-statement int java.util.scanner


【解决方案1】:

您可以尝试使用 user.equals("1") 而不是 Integer.parseInt(user) == 1。或者假设异常 java.lang.NumberFormatException 有效并捕获该异常并以正确的方式处理它,例如:

    System.out.println("where would you like to send the following item: ");
    System.out.println("1. groceries, 2. savings, 3. utilities");
    System.out.print("Enter list name or list number to send: ");
    String user = user_input.nextLine();
    try {
          if (user.toLowerCase().equals("groceries") || Integer.parseInt(user) == 1) {
                 System.out.println("item was sent to the grocery list");

          }else if(user.toLowerCase().equals("savings") || Integer.parseInt(user) == 2) {
                 System.out.println("item was sent to the savings list");

          }else if(user.toLowerCase().contains("utilities") || Integer.parseInt(user) == 3) {
                 System.out.println("item was sent to the utilities list");

          }else{
                 System.out.println("item is not in any category");

          }
    } catch (NumberFormatException nfe) {
        System.out.println("item is not in any category");
    }

更新:

    System.out.println("where would you like to send the following item: ");
    System.out.println("1. groceries, 2. savings, 3. utilities");
    System.out.print("Enter list name or list number to send: ");
    String user = user_input.nextLine();
    if (user.toLowerCase().equals("groceries") || user.equals("1")) {
        System.out.println("item was sent to the grocery list");

    }else if(user.toLowerCase().equals("savings") || user.equals("2")) {
        System.out.println("item was sent to the savings list");

    }else if(user.toLowerCase().contains("utilities") || user.equals("3")) {
        System.out.println("item was sent to the utilities list");

    }else{
        System.out.println("item is not in any category");

    }

【讨论】:

  • 这行不通。例如,如果输入是“储蓄”会发生什么?
  • 你是对的。所以对你来说正确的解决方案是使用 equals 而不是 Integer.paseInt。将编辑我的解决方案。
【解决方案2】:

我不是 100% 确定,但问题可能是当输入一个字符串时,它仍然尝试解析一个 int,导致它失败。例如,输入“2”是有效的,因为“2”是一个有效的字符串,而 2 是一个可以从字符串“2”解析的有效 int,而“groceries”是一个有效的字符串,但不包含可以被解析。

【讨论】:

  • 您可以在 if 语句之前使用 try-catch 块。 int x;try (x = Integer.parseInt(user)) {// If statements for integer here}catch(NumberFormatException e) {If statements for string here}(抱歉格式化,不太对劲)
【解决方案3】:

Odd822 是对的。问题是当您输入除“groceries”以外的任何字符串时,程序会尝试使用 Integer.parseint("stringwhichisnotaninteger")。 因此,它会引发异常。

这段代码应该可以工作:

if (user.toLowerCase().equals("groceries") || user.equals("1") ) {
        System.out.println("item was sent to the grocery list");

    }else if(user.toLowerCase().equals("savings") || user.equals("2")) {
        System.out.println("item was sent to the savings list");

    }else if(user.toLowerCase().contains("utilities") || user.equals("3")) {
        System.out.println("item was sent to the utilities list");

    }else{
        System.out.println("item is not in any category");

    }

【讨论】:

  • 他说使用 user.equals 会导致上面的另一个问题,所以我只是假设这样会发生一些问题。现在编辑我的答案。
  • 当您比较包含大写字母的单词(例如“Groceries”)时,这可能会导致问题,但对于数字来说不是问题。感谢您的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
相关资源
最近更新 更多