简体   繁体   English

您如何在异常消息Java中包括用户输入?

[英]How do you include user input in exception message Java?

Is it possible, in Java, to include a user's input into an exception message while also using the valuse as an int or double ? 在Java中,是否可以将用户的输入包含在异常消息中,同时还可以将valuse用作intdouble For example, they should enter a number (eg, 5) but instead fat-finger something like (5r). 例如,他们应该输入数字(例如5),但是要用胖手指输入(5r)。

Is it possible to have a message say something like "You entered 5r, but should have entered a number."? 是否可能会显示类似“您输入了5r,但应该输入数字”之类的消息?

I tried using the traditional way of getting variables to print in Java here: 我尝试使用传统的方式来获取变量以在Java中打印:

try {
  System.out.println();
  System.out.println("Value: ");
  double value = sc.nextDouble();sc.nextLine();
  exec.ds.push(value);
} 
catch (Exception e1) {
    System.out.println(e1+ "You entered:  " + value + ": You must enter a number");                            
}

In this case, I am trying to get value displayed in the Exception message, where value is the user's invalid input. 在这种情况下,我想获得value异常消息,其中值是用户的无效输入显示。

At value it gives the error cannot find symbol . value给出错误cannot find symbol

Below is the answer I came up with. 以下是我想出的答案 The reason I choose this answer was because most of your answers produced the output, "You entered 0.0 ..." because the answer must first be a string to be printed in the console. 之所以选择此答案,是因为大多数答案都产生了输出“您输入了0.0 ...”,因为答案必须首先是要在控制台中打印的字符串。 In addition, to be used as a number elsewhere in the program the String has cast to an Object of type Double or Integer : 另外,为了在程序中的其他地方用作数字,字符串已转换为Double或Integer类型的Object:

String value = null; //declared variable outside of try/catch block and initialize. 
try {
  System.out.println();
  System.out.println("Value: ");
  value = sc.nextLine(); //Accept String as most of you suggested
  Double newVal = new Double(value); //convert the String to double for use elsewhere
  exec.ds.push(newVal); //doulbe is getting used where it would be a 'number' instead of string

} 
catch (Exception e1) {
    System.out.println(e1+ "You entered:  " + value + ": You must enter a number"); //the String valuse is received and printed as a String here, not a 'number'            
} //now prints what the users inputs...

value it gives the error cannot find symbol. 值,它给出错误找不到符号。

You have declare the value in local block. 您已经在本地块中声明了值。 And you are trying to access it outside the block. 您正在尝试在块外访问它。

Is it possible to have a message say something like "You entered 5r, but should have entered an number. 是否可能会显示一条消息,例如“您输入了5r,但应该输入了一个数字。

Better option is to use flag variable and loop until user enter correct number. 更好的选择是使用标志变量并循环直到用户输入正确的数字。

boolean flag = true;
double value = 0;
while(flag)
{
    try {
        System.out.println();
        System.out.println("Value: ");
        value = sc.nextDouble();
        exec.ds.push(value);
        flag = false;
    } 
    catch (Exception e1) {
        System.out.println(e1+ "You entered:  " + value + ": You must enter a number");                            
    }
}

Get user input: 获取用户输入:

String input = '';

try{

    Console console = System.console();
    input = console.readLine("Enter input:");
}

... then in your catch you could do: ...然后您可以执行以下操作:

catch (Exception e1) {
    System.out.println(e1+ "You entered:  " + input + ": You must enter a number");                            
  }

Other than the above, I don't see what the problem is. 除上述以外,我看不出问题所在。 You can google how to get user input in Java, and then just take the input, put it in a variable, and print it out on error. 您可以使用Google搜索如何用Java获取用户输入,然后仅接受输入,将其放入变量中,并在出现错误时将其打印出来。

This does not work, because value is no longer in scope in the catch block. 这不起作用,因为value不再在catch块的范围内。 Instead you can declare it before the try: 相反,您可以在尝试之前声明它:

double value = 0;
try {
  System.out.println();
  System.out.println("Value: ");
  value = sc.nextDouble();sc.nextLine();
  exec.ds.push(value);
} catch (Exception e1) {
  System.out.println(e1+ "You entered:  " + value + ": You must enter a number");                            
}

This will however not help if the exception is thrown because of a syntax error in the double. 但是,如果由于双精度语法错误而引发异常,这将无济于事。 To handle this, you need to read a String and then convert to a double . 要处理此问题,您需要读取String ,然后转换为double

In this case - no, as the calling of sc.nextLine() causes the exception to be thrown, so no value is written to variable value . 在这种情况下-否,因为调用sc.nextLine()会引发异常,因此不会将任何值写入变量value

Moreover, value is a local variable in this case and is unavailable from other blocks of code. 而且,在这种情况下, value是局部变量,其他代码块中不存在该value

According to the documentation nextDouble() : 根据文档 nextDouble()

Scans the next token of the input as a double. 将输入的下一个标记扫描为double。 This method will throw InputMismatchException if the next token cannot be translated into a valid double value. 如果下一个标记不能转换为有效的double值,则此方法将引发InputMismatchException

So, if the input cannot be translated into a double, an exception is thrown. 因此,如果输入不能转换为双精度型,则会引发异常。 If you want to provide the inputted string in the exception message, I suggest you read the line as a string and try to parse it to a double, and if that fails, you can include the read line in the exception message. 如果要在异常消息中提供输入的字符串,建议您将该行作为字符串读取,并尝试将其解析为双精度,如果失败,则可以在异常消息中包括读取的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM