简体   繁体   中英

Trouble in using different packages in same project in Net Beans IDE

I am a school student, and I am using Net beans IDE to write java programs, and not create projects.

Now, I am facing this problem. I have two packages in the same project. One is a user defined utility package that I created. This has one program that has methods defined for printing and taking double dimensional arrays as input. It has no main method.

A small part of my class is given below. I am giving only that part in which I am facing problems.

package UserDefinedUtilities;
import java.util.*;
public class ArrayUtilities2D {
  public int[][] InputInt(int m, int n){
      Scanner kb = new Scanner (System.in);
      System.out.println();
      int arr[][] = new int[m][n];
      System.out.println("Enter the array elements: ");
      for (int i = 0; i < m; i++){
          for (int j = 0; j < n; j++){
              System.out.print("Enter the element in cell (" + i + "," + j + "): ");
              arr[i][j] = kb.nextInt();
          }
      }
      kb.close();
      System.out.println();
      return arr;
  }
}

This is the input method of the class from where I am trying to access.

package AnswerPrograms;
import UserDefinedUtilities.*;
import java.util.*;
public class J124{
    private int m, n;
    private void Input(){
        Scanner kb = new Scanner (System.in);
        System.out.print("Enter the number of rows: ");
        m = kb.nextInt();
        System.out.print("Enter the number of columns: ");
        n = kb.nextInt();
        kb.close();
        int arr[][] = new ArrayUtilities2D().InputInt(m, n); //using the input method from the above class
        System.out.println("The original matrix is: ");
        new ArrayUtilities2D().IntPrint(m, n, arr);
        if (m % 2 == 0){
            Mirror_Even(arr);
        }
        else{
            Mirror_Odd(arr);
        }
    }
    . //other necessary methods are present here
    .
    .
    .
}

In the second package, I store my programs. Now, when I try to take input from this class using this very method, the following lines are shown:

Exception in thread "main" java.util.NoSuchElementException
Enter the element in cell (0,0):    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 UserDefinedUtilities.ArrayUtilities2D.InputInt(ArrayUtilities2D.java:24)
at AnswersPrograms.J124.Input(J124.java:13)
at AnswersPrograms.J124.main(J124.java:90)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

Can anyone explain why this is coming? I am not facing this problem in BlueJ. Why is there a NoSuchElementException even before I am entering something? What should I do to rectify this? Should I change my jdk?

The error has nothing to do with packages. You code compiles and runs fine, and as the stack trace shows, both methods are invoked.

The problem is that you're trying to read, using the scanner, from System.in , but you've closed it before reading. So there's nothing to be read anymore.

As the javadoc says :

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked.

So, when you close the scanner, you're also closing System.in .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM