1

program to input 2d array using Scanner class but output shows:

Runtime error:
 NZEC
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 Wocode.main(Main.java:9)

Code snippet:

import java.lang.*;
    import java.util.*;
    import java.util.Scanner;

     class Wocode {
        public static void main(String args[]) {
            Scanner s=new Scanner(System.in);
                int m=s.nextInt();
                int n=s.nextInt();
                int A[][] = new int[m][n];
                for(int i=0;i<m;i++)
                {
                  for(int j=0;j<n;j++)
                    {
                     A[i][j]=s.nextInt();
                    }
                }     
    }
    }
catch23
  • 13,661
  • 38
  • 120
  • 194

4 Answers4

1

Don't use nextInt() as it doesn't clean the line in the buffer. So, a better way to the user input is to use s.nextLine(). To take integer input do

Integer.parseInt(s.nextLine())

When you use nextInt() it takes the input but the line after the input is still there in the buffer and when the another nextInt() looks for the input you get the exception.

shelholmes221
  • 348
  • 3
  • 15
0

I agree to cricket_007. Need to check hasNext() method before calling s.next() as below. <if(s.hasNextInt()){ A[i][j]= s.nextInt(); }>

Krishna
  • 21
  • 3
0

Instead of calling hasNextInt, you should use a blocking readLine call and cast to an integer instead.

int n = Integer.parseInt(s.nextLine());
washcloth
  • 2,322
  • 13
  • 27
0

Your code has a few mistakes.

Here is a sequence, how to solve them:

  • you have to say to a user what exactly the program expects as input
  • validate if the user entered correct values
  • parse entered values
  • use that values

You didn't do some steps. Therefore, your solution isn't even executed.

Here is some code snippet, which fits all steps:

class Wocode {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter two numbers (separated by space): ");

        if (scanner.hasNext()) {
            String[] input = scanner.nextLine().split(" ");
            if (input.length == 2) {
                int firstNumber = Integer.valueOf(input[0]);
                int secondNumber = Integer.valueOf(input[1]);

                int arr[][] = new int[firstNumber][secondNumber];

                for (int rows = 0; rows < firstNumber; rows++) {
                    for (int columns = 0; columns < secondNumber; columns++) {
                        arr[rows][columns] = firstNumber + secondNumber;
                        System.out.print(String.format("%4s", arr[rows][columns]));
                    }
                    System.out.println();
                }
            } else {
                System.err.println("You have to enter two digits, separated by space!");
            }
        }
    }
}

Output:

Please enter two numbers (separated by space): 3 5
   8   8   8   8   8
   8   8   8   8   8
   8   8   8   8   8

Useful links:

catch23
  • 13,661
  • 38
  • 120
  • 194