0

I am getting the following error and can't work out why it is thrown:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at exam.main(exam.java:11)

Here is the code:

import java.util.*;
public class Exam {

    public static void main(String[] args) {
        int a;
        int b;
        Random rd=new Random();
        Scanner input  = new Scanner(System.in);
        System.out.println("Enter the number of rows");
        a=input.nextInt();
        System.out.println("Enter the number of columns");
        b=input.nextInt();

        int[][]arr=new int[a][b];
        int x = arr.length;
        int y = arr[0].length;
        for(int i=0;i<x;i++) {
            for(int c=0;c<y;c++) {
                arr[i][c]=rd.nextInt();
            }
        }
        for(int i=0;i<x;i++) {
            for(int c=0;c<y;c++) {
                System.out.println(arr[i][c]);
            }
        }
    }
}
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • 3
    Possible duplicate of [How to use java.util.Scanner to correctly read user input from System.in and act on it?](https://stackoverflow.com/questions/26446599/how-to-use-java-util-scanner-to-correctly-read-user-input-from-system-in-and-act) – Justin Albano Apr 10 '18 at 23:49
  • 1
    `echo "3 4" | java Exam` works for me. (Note a: Since the canonical way to name classes with a leading uppercase, I changed that, but it is orthogonal to the question. Note b: I inserted an `import java.util.*;` to make the code compile. Why do so many users delete the imports? Just for making it not too easy to answer questions? How is the missing import related to the line number of the error message? Well - now we have to guess - was it corrected?) Ah - now I see, Mr. @Bentaye removed the import. Bad idea! – user unknown Apr 11 '18 at 14:17
  • @userunknown I think the import was `java.util.*`, I didn't think it was of importance. I think it is usually better to not have the imports when they are obvious (it improves readability). But I agree that it messes up the line count. – Bentaye Apr 11 '18 at 14:22
  • Since the program works for me, you have to tell us what the input is. I can reproduce your error by giving too few inputs: `echo "3 " | java Exam` but with "3 4", it works fine. – user unknown Apr 11 '18 at 14:23
  • 1
    @Bentaye: How should it improve readability? I often copy code to test myself, what the problem is, and constantly have to add missing imports. I'm just loosing time. See `mcve` and don't delete imports. Instead, you should add them where they are missing. Sometimes imports are even significant (util.Random vs. math.Random, util.List vs. swing.List, different logger implementations and so on). – user unknown Apr 11 '18 at 14:27
  • @userunknown I get your point – Bentaye Apr 11 '18 at 14:29

0 Answers0