Luke Ndatigh
Luke Ndatigh

Reputation: 91

JAVA Matrix Program printing only 0s and not accepting input

My program runs without errors. But, skips the readInput method somehow. Which results in printing only 0s.

Also, how do I modify my displayResult method in order to make my output more "matrix-like" in display.

My source code below.

import java.util.Scanner;
public class Matrices {
    int i, j, k, n = 3;
    int[][] matA = new int[n][n];
    int[][] matB = new int[n][n];
    int[][] matSum = new int[n][n];
    int[][] matProd = new int[n][n];

    public void readInput() {
        Scanner scan = new Scanner(System.in);
        for (i = 0; n < 3; i++) {
            for (j = 0; j < n; j++) {
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
            }
        }
    }

    public void findSum() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                matSum[i][j] = matA[i][j] + matB[i][j];
            }
        }
    }

    public void findProduct() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    matProd[i][j] = matProd[i][j] + matA[i][j] * matB[i][j];
                }
            }
        }
    }

    public void displayResult() {
        // Printing the Sum Matrix
        System.out.println("Sum Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matSum[i][j] + " ");
            }
        }

        // Printing the Product Matrix
        System.out.println("Product Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matProd[i][j] + " ");
            }
        }
        System.out.println();
    }

public static void main(String[] args){

    System.out.println("Matrix Calculator");
    System.out.println("-------------------\n");
    Matrices self = new Matrices();

    self.readInput();
    self.findSum();
    self.findProduct();
    self.displayResult();
}
}

Upvotes: 2

Views: 80

Answers (3)

R Dhaval
R Dhaval

Reputation: 546

You did a mistake here in for loop

int i, j, k, n=3;
int[][]matA = new int [n][n];
int[][]matB = new int [n][n];
int[][]matSum = new int [n][n];
int[][]matProd = new int [n][n];

public void readInput(){
    Scanner scan = new Scanner(System.in);
    for(i = 0; n < 3; i++){
        for(j = 0; j < n; j++){ ...

here n<3 will evaluate to 3<3 which logically returns false and control never goes inside of first for loop and neither your nextInt gets executed.

so replace n<3 with i<3

Upvotes: 2

kk.
kk.

Reputation: 3945

Please use the below code:

import java.util.Scanner;

public class Matrices {

    int i, j, k, n = 3;
    int[][] matA = new int[n][n];
    int[][] matB = new int[n][n];
    int[][] matSum = new int[n][n];
    int[][] matProd = new int[n][n];

    public void readInput() {
        Scanner scan = new Scanner(System.in);
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
                System.out.println("matB[" + i + "][" + j + "]");
                matB[i][j] = scan.nextInt();
            }
        }
    }

    public void findSum() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                matSum[i][j] = matA[i][j] + matB[i][j];
            }
        }
    }

    public void findProduct() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    matProd[i][j] = matProd[i][j] + matA[i][j] * matB[i][j];
                }
            }
        }
    }

    public void displayResult() {
        // Printing the Sum Matrix
        System.out.println("Sum Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matSum[i][j] + " ");
            }
        }

        // Printing the Product Matrix
        System.out.println("Product Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matProd[i][j] + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {

        System.out.println("Matrix Calculator");
        System.out.println("-------------------\n");
        Matrices self = new Matrices();

        self.readInput();
        self.findSum();
        self.findProduct();
        self.displayResult();
    }
}

Upvotes: 1

Your read input method is not working because of this:

 for (i = 0; n < 3; i++) {

since n is declared and init to 3 the condition n < 3 returns false, so the matrix is never filled, and everything after that is jsut based on a zero matrix!

Upvotes: 5

Related Questions