Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you can see the nature of <a href="http://www.cs.utexas.edu/users/ndale/Scanner.html" rel="nofollow noreferrer">Scanner</a>:</p> <blockquote> <p>double nextDouble()</p> <p>Returns the next token as a double. <strong>If the next token is not a float or is out of range, InputMismatchException is thrown.</strong></p> </blockquote> <p>Try to catch the exception</p> <pre><code>try { // ... } catch (InputMismatchException e) { System.out.print(e.getMessage()); //try to find out specific reason. } </code></pre> <p><strong>UPDATE</strong></p> <p><strong>CASE 1</strong></p> <p>I tried your code and there is nothing wrong with it. Your are getting that error because <strong>you must have entered <code>String</code> value</strong>. When I entered a numeric value, it runs without any errors. But once I entered <code>String</code> it <code>throw</code> the same <code>Exception</code> which you have mentioned in your question. </p> <p><strong>CASE 2</strong></p> <p>You have entered something, which is <strong>out of range</strong> as I have mentioned above. </p> <p>I'm really wondering what you could have tried to enter. In my system, it is running perfectly without changing a single line of code. Just copy as it is and try to compile and run it. </p> <pre><code>import java.util.*; public class Test { public static void main(String... args) { new Test().askForMarks(5); } public void askForMarks(int student) { double marks[] = new double[student]; int index = 0; Scanner reader = new Scanner(System.in); while (index &lt; student) { System.out.print("Please enter a mark (0..30): "); marks[index] = (double) checkValueWithin(0, 30); index++; } } public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num &lt; min || num &gt; max) { System.out.print("Invalid. Re-enter number: "); num = reader.nextDouble(); } return num; } } </code></pre> <p>As you said, you have tried to enter <code>1.0</code>, <code>2.8</code> and etc. Please try with this code.</p> <p><strong>Note : Please enter number one by one, on separate lines. I mean, enter <code>2.7</code>, press enter and then enter second number (e.g. <code>6.7</code>).</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload