如何在同一用户输入中接受字符串或整数

时间:2014-04-12 09:51:30

标签: java user-input

我想在Java中接受用户输入为整数或字符串,但无论我做什么,我总是会收到错误。

我的代码非常简单(我是初学者):

    System.out.println(
      "Enter the row number (or enter e to quit the application):"
    );

    Scanner rowInput = new Scanner(System.in);
    int row1 = rowInput.nextInt();

我希望用户也能够按下" e"退出。

我尝试了几件事:

1)将row1转换为String并说出:

    if((String)(row1).equals("e"){
    System.out.println("You have quit") }

2)转换" e"到整数并说:

    if(row1 == Integer.ParseInt("e"){
    System.out.println("You have quit") }

3)要执行switch语句,但它说我有不兼容的类型(Stringint)。

我的错误通常是:Exception in thread "main" java.util.InputMismatchException

有人可以帮助我吗?

非常感谢提前!

6 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情

    Scanner rowInput = new Scanner(System.in);
    String inputStr = rowInput.nextLine();

    try{
        int row1 = Integer.parseInt(inputStr);
    } catch (NumberFormatException e) //If exception occurred it means user has entered 'e'
    {
        if ("e".equals(inputStr)){
            System.out.println("quiting application");
        }
     }

您应该将扫描仪的输入读作String类型,然后尝试使用StringInteger.parseInt()输入转换为整数。

如果它成功解析,则意味着用户输入了一个Integer。 如果失败,它将抛出异常NumberFormatException,它会告诉你它不是整数。所以你可以继续检查它e如果是的话,根据你的要求做你想做的事。

答案 1 :(得分:0)

1)您应该说Integer.parseInt()而不是Integer.ParseInt()

2)如果您将“e”传递给Integer.parseInt(),您将获得java.lang.NumberFormatException

3)以这种方式获得string的输入(因为它更安全)*:

Scanner rowInput = new Scanner(System.in);
String row = rowInput.next();
if (row.equals("e")) {
    //do whatever
}
else if(row.matches("\\d+$")) {
    //do whatever
}

*如果用户输入非整数输入,您将遇到java.util.InputMismatchException

答案 2 :(得分:0)

如果您不确定是否有号码,则无法使用nextInt()。此外,您无法将e解析为整数,因为它不是整数,它是char或string(在这种情况下我建议使用字符串)。

这意味着,您的代码应如下所示:

System.out.println("Enter the row number (or enter e to quit the application):");

Scanner rowInput = new Scanner(System.in);
string row1 = rowInput.next();

然后你有一个字符串中的数据。您只需检查字符串是否为e:

if (row1.Equals("e")) ...

检查输入实际上是否为整数也是一个好主意。检查它的好方法在这里描述:

How to check if a String is numeric in Java

答案 3 :(得分:0)

试试这个:

public class Demo{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String choice = null;
        System.out.println("Enter e to exit or some other key to stay:");
        choice=s.next();

        if(choice.equals("e"))
        {
            System.out.println("quits");
            System.exit(0);
        }
        else
        {           
            System.out.println("stays");
        }
    }
}

答案 4 :(得分:0)

  

我尝试了几件事......

试图通过“尝试”来解决这个问题是错误的做法。

正确的方法是了解正在发生的事情,然后修改您的解决方案以考虑到这一点。

你遇到的问题是你有一行输入,可以是一个数字或特殊值e,这意味着退出。 (事实上​​,它也可能是其他几件事......但我会谈到这一点。)

当您尝试:

  • 当下一个输入不是整数时调用Scanner.nextInt(),或者

  • 对非整数字符串

  • 调用Integer.parseInt(...)

你会得到一个例外。那你做什么?

一种方法是改变你正在做的事情,这样你就不会在那些情况下进行这些调用。例如:

  • 在调用Scanner.hasInt()之前调用nextInt()以查看下一个标记是否为整数,或

  • 在尝试将String转换为整数之前测试"e"大小写。

另一种处理此问题的方法是捕获异常。例如,你可以这样做:

    String s = ...
    try {
        number = Integer.parseInt(s);
    } catch (NumberFormatException ex) {
        if (s.equals("e")) {
            ...
        }
    }

无论哪种方式都有效,但我会留下你来解决细节问题。

但我想说的是,解决这个问题的正确方法是了解原始版本/版本中发生的事情,并根据您的理解,修改您正在做的事情。如果您只是随机“尝试”您在Google上找到的替代方案,那么您将无法进入成为高效程序员的程度。


我说还有其他情况。他们是:

  • 用户输入的内容既不是您的特殊e字符。它可能是任何东西。一个空行,hi mum ...

  • 用户键入“文件结束”字符;例如Linux上为CTRL-D,Windows上为CTRL-Z

如果您希望您的程序健壮,您也需要处理这些案例。

答案 5 :(得分:-1)

您无法将字符串转换为整数,使用字符ASCII代码。