扫描仪:扫描int或字符串

时间:2018-01-02 18:32:26

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

尝试使用扫描程序获取int或字符串..下面的代码就是我所拥有的:

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将会起作用,但它仍然会给我错误消息。

非常感谢任何帮助...

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

static navigationOptions = {
drawerLabel: () => null

<强>更新

    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");
    }

答案 2 :(得分:0)

Odd822是对的。问题是当你输入&#34; groceries&#34;以外的任何字符串时,程序会尝试Integer.parseint(&#34; stringwhichisnotaninteger&#34;)。 因此,它会引发异常。

此代码应该有效:

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");

    }