简体   繁体   中英

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 ? For example, they should enter a number (eg, 5) but instead fat-finger something like (5r).

Is it possible to have a message say something like "You entered 5r, but should have entered a number."?

I tried using the traditional way of getting variables to print in Java here:

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.

At value it gives the error 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. In addition, to be used as a number elsewhere in the program the String has cast to an Object of type Double or Integer :

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.

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.

This does not work, because value is no longer in scope in the catch block. 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 .

In this case - no, as the calling of sc.nextLine() causes the exception to be thrown, so no value is written to variable value .

Moreover, value is a local variable in this case and is unavailable from other blocks of code.

According to the documentation nextDouble() :

Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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