Note that there are some explanatory texts on larger screens.

plurals
  1. PONoSuchElementException with Java.Util.Scanner
    primarykey
    data
    text
    <p>I am very new to Java but am working through the book Java: How to program (9th ed.) and have reached an example where for the life of me I cannot figure out what the problem is.</p> <p>Here is a (slightly) augmented version of the source code example in the textbook:</p> <pre><code>import java.util.Scanner; public class Addition { public static void main(String[] args) { // creates a scanner to obtain input from a command window Scanner input = new Scanner(System.in); int number1; // first number to add int number2; // second number to add int sum; // sum of 1 &amp; 2 System.out.print("Enter First Integer: "); // prompt number1 = input.nextInt(); // reads first number inputted by user System.out.print("Enter Second Integer: "); // prompt 2 number2 = input.nextInt(); // reads second number from user sum = number1 + number2; // addition takes place, then stores the total of the two numbers in sum System.out.printf( "Sum is %d\n", sum ); // displays the sum on screen } // end method main } // end class Addition </code></pre> <p>I am getting the 'NoSuchElementException' error:</p> <pre><code>Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:838) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at Addition.main(Addition.java:16) Enter First Integer: </code></pre> <p>I understand that this is probably due to something in the source code that is incompatible with the <code>Scanner</code> class from <code>java.util</code>, but I really can't get any further than this in terms of deducing what the problem is.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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