简体   繁体   English

NoSuchElementException发生在简单的Java程序中以添加两个矩阵吗?

[英]NoSuchElementException occuring in simple java program for addition of two matrices?

Below is the program to add two matrices : 下面是添加两个矩阵的程序:

import java.util.Scanner;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
public class addmatrix {
    int row,col;
    int v[][]=new int[100][100];

    public addmatrix(int i, int j) {
        // TODO Auto-generated constructor stub
        row=i;
        col=j;
    }
    public void display(){
        for(int d=0;d<row;d++){
            for(int e=0;e<col;e++){
                System.out.print(v[d][e]);
            }
        System.out.println(" ");
        }
    }
    public void getmat(){   
        Scanner iny=new Scanner(System.in);
        for(int d=0;d<row;d++)
            for(int e=0;e<col;e++){
                System.out.println("Enter the element:");
                v[d][e]=iny.nextInt();
            }
        iny.close();
        }
    public addmatrix add(addmatrix m){
        addmatrix ans=new addmatrix(m.row,m.col);
        for(int d=0;d<m.row;d++)
            for(int e=0;e<m.col;e++){
                ans.v[d][e]=v[d][e]+m.v[d][e];
            }
        return ans;
    }

    /**
     * @param args
     */
    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Enter the no. of rows: ");
        Scanner inp=new Scanner(System.in);
        int i=0,j=0;
        i=inp.nextInt();
        System.out.println("Enter the no. of column: ");
        j=inp.nextInt();
        addmatrix m1=new addmatrix(i,j);
        m1.getmat();
        m1.display();
        addmatrix m2=new addmatrix(i,j);
        m2.getmat();
        System.out.print("+");
        m2.display();
        addmatrix m3=new addmatrix(i,j);
        m3=m1.add(m2);
        System.out.print("=");
        m3.display();
    }

}

There is no exception occuring when getmat() is called for the first time using m1, but when again I'm creating a object m2 and if I'm calling getmat() using m2 then it throws the exception. 当第一次使用m1调用getmat()时,没有发生异常,但是当我再次创建对象m2时,如果我使用m2调用getmat(),它将引发异常。

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at addmatrix.getmat(addmatrix.java:26)
    at addmatrix.main(addmatrix.java:54)

I don't understand why this exception is occuring. 我不明白为什么会发生这种异常。

In your getmat function you close the Scanner. 在您的getmat函数中,关闭扫描仪。 This also closes the underlying source, which in your case is System.in. 这也将关闭基础源,在您的情况下为System.in。 Because you closed System.in, you can no longer read from it. 由于您关闭了System.in,因此无法再读取它。

I suggest you only create one Scanner and use it for all calls of getmat. 我建议您仅创建一个Scanner并将其用于所有getmat调用。

And also, please use camelcase! 另外,请使用驼峰箱!

You could do it this way: 您可以这样进行:

import java.util.Scanner;

public class AddMatrix {
    int row, col;
    int values[][];

    public AddMatrix(int i, int j) {
        row = i;
        col = j;
        this.values = new int[i][j];
    }

    public void display() {
        for (int d = 0; d < row; d++) {
            for (int e = 0; e < col; e++) {
                System.out.print(values[d][e]);
            }
            System.out.println(" ");
        }
    }

    public void getMat(Scanner scanner) {
        for (int d = 0; d < row; d++)
            for (int e = 0; e < col; e++) {
                System.out.println("Enter the element:");
                values[d][e] = scanner.nextInt();
            }
    }

    public AddMatrix add(AddMatrix m) {
        AddMatrix ans = new AddMatrix(m.row, m.col);
        for (int d = 0; d < m.row; d++)
            for (int e = 0; e < m.col; e++) {
                ans.values[d][e] = values[d][e] + m.values[d][e];
            }
        return ans;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("Enter the no. of rows: ");
        Scanner scanner = new Scanner(System.in);
        int i = 0, j = 0;
        i = scanner.nextInt();
        System.out.println("Enter the no. of column: ");
        j = scanner.nextInt();
        AddMatrix m1 = new AddMatrix(i, j);
        m1.getMat(scanner);
        m1.display();
        AddMatrix m2 = new AddMatrix(i, j);
        m2.getMat(scanner);
        System.out.print("+");
        m2.display();
        AddMatrix m3 = new AddMatrix(i, j);
        m3 = m1.add(m2);
        System.out.print("=");
        m3.display();
        scanner.close();
    }
}

Note: I also changed your array initialization to be exactly the entered size instead of 100x100. 注意:我还将数组初始化更改为恰好输入的大小,而不是100x100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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