在简单的java程序中出现NoSuchElementException以添加两个矩阵?

时间:2016-03-30 14:22:34

标签: java matrix

以下是添加两个矩阵的程序:

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();
    }

}

使用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)

我不明白为什么会发生这种异常。

1 个答案:

答案 0 :(得分:1)

在getmat功能中关闭扫描仪。这也会关闭底层源代码,在您的情况下是System.in。因为您关闭了System.in,所以您无法再从中读取。

我建议您只创建一个扫描仪并将其用于所有getmat调用。

另外,请使用camelcase!

你可以这样做:

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();
    }
}

注意: 我还将数组初始化更改为输入的大小而不是100x100。