title: How do you include user input in exception message Java?
body: <p>Is it possible, in Java, to include a user's input into an exception message while also using the valuse as an <code>int</code> or <code>double</code>? For example, they should enter a number (e.g., 5) but instead fat-finger something like (5r). </p>
  
  <p>Is it possible to have a message say something like "You entered 5r, but should have entered a number."?</p>
  
  <p>I tried using the traditional way of getting variables to print in Java here: </p>
  
  <pre><code>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");                            
  }
  </code></pre>
  
  <p>In this case, I am trying to get <code>value</code> displayed in the Exception message, where value is the user's invalid input.</p>
  
  <p>At <code>value</code> it gives the error <code>cannot find symbol</code>.</p>
  
  <p>Below is the <strong>answer</strong> 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 : </p>
  
  <pre><code>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...
  </code></pre>