扫描仪:扫描整数或字符串



尝试使用扫描仪获取整数或字符串..下面的代码是我所拥有的:

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

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

任何帮助都非常感谢...

你可以尝试做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");
    }

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

Odd822 是对的。问题是,当你输入除"杂货"以外的任何字符串时,程序会尝试使用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");
    }

最新更新