1

After getting the program to work for the most part (the help is appreciated :)). All I needed was to add the while loop so the user gets the menu option as many times he wants to. It starts giving me errors as "; expected" at the line where continue switches to false. Here is the code.

import java.io.*;
import java.util.*;

class Matrix {

    double[][] element;
    int rows, cols;

    Matrix(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        element = new double[rows][cols];
    }

    public double getValue(int row, int col) {
        return element[row][col];
    }

    public void setValue(int row, int col, double value) {
        element[row][col] = value;
    }

    public int getNoRows() {  // returns the total number of rows
        return rows;
    }

    public int getNoCols() { // returns the total number of cols
        return cols;
    }

     // The methods for the main calculations
     public Matrix AddMatrix(Matrix m2) {
        int row1 = getNoRows();
        int col1 = getNoCols();
        Matrix result = new Matrix(row1, col1);

        for (int i = 0; i < row1; i++) {
            for (int j = 0; j < col1; j++) {
                result.setValue(i, j, this.getValue(i, j) + m2.getValue(i, j));
            }
        }
        return result;
    }

    public Matrix MultiplyMatrix(Matrix m2) {
        if (this.getNoCols() != m2.getNoRows()) {
            throw new IllegalArgumentException("matrices can't be multiplied");
        }
        int row2 = this.getNoRows();
        int col2 = m2.getNoCols();
        Matrix result = new Matrix(row2, col2);
        for (int i = 0; i < row2; i++) {
            for (int j = 0; j < col2; j++) {
                result.setValue(i, j, result.getValue(i, j) + this.getValue(i, j) * m2.getValue(i, j));
            }
        }
        return result;

    }

    public Matrix TransposeMatrix() {
        int row3 = this.getNoCols();
        int col3 = this.getNoRows();
        Matrix result = new Matrix(row3, col3);
        for (int i = 0; i < row3; i++) {
            for (int j = 0; j < col3; j++) {
                result.setValue(i, j, this.getValue(j, i));
            }
        }
        return result;

    }

    public void DisplayMatrix() {
        for (int i = 0; i < this.getNoRows(); i++) {
            for (int j = 0; j < this.getNoCols();
                    j++) {
                System.out.print((this.getValue(i, j)) + " ");
            }
            System.out.print("\n");
        }
      }
}


public class Lab1 {

    public static void main(String args[]) throws FileNotFoundException {

        int choice;
        Scanner in = new Scanner(System.in);


        Boolean continue = true;
        while (continue){   

        System.out.println("Enter your choice /n");
        choice = in.nextInt();

        System.out.println("1. Add two matrices \n");
        System.out.println("2. Multiplymatrix two matrices \n");
        System.out.println("3. Take transpose of a matrix \n");
        System.out.println("4. Display a matrix \n");
        System.out.println("5. Exit \n");

        if (choice == 1) {
            Matrix m1 = MatrixReader();
            m1.DisplayMatrix();
            Matrix m2 = MatrixReader();
            m2.DisplayMatrix();
            Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
            m3 = m1.AddMatrix(m2);
            m3.DisplayMatrix();
        }

        if (choice == 2) {
            Matrix m1 = MatrixReader();
            m1.DisplayMatrix();
            Matrix m2 = MatrixReader();
            m2.DisplayMatrix();
            Matrix m3 = new Matrix(m1.getNoRows(), m2.getNoCols());
            m3 = m1.MultiplyMatrix(m2);
            m3.DisplayMatrix();
        }



        if (choice == 3) {
            Matrix m1 = MatrixReader();
            m1.DisplayMatrix(); 
            Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
            m3 = m1.TransposeMatrix();
            m3.DisplayMatrix();
        }

        if (choice == 4) {
            System.out.println("Will need to call the DisplyMatrix method for the object \n");
        } 

        if (choice == 5) {
            continue = false;
        }
        else {
            System.out.println("Incorrect input. Kindly enter again \n");
        }
      }
    }

    public static Matrix MatrixReader() throws FileNotFoundException {
        System.out.println("Give the filename for the matrix");
        Scanner filescanner = new Scanner(System.in);
        Scanner scanner = new Scanner(new File(filescanner.nextLine()));
        scanner.nextLine(); // removes the first line in the input file
        String rowLine = scanner.nextLine();
        String[] arr = rowLine.split("=");
        int rows = Integer.parseInt(arr[1].trim());

        String colLine = scanner.nextLine();
        String[] arr2 = colLine.split("=");
        int cols = Integer.parseInt(arr2[1].trim());
        Matrix test = new Matrix(rows, cols);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                test.setValue(i, j, scanner.nextDouble());
            }
        }


        return test;

   }
}
2
  • 4
    isn't continue a predefined type, change it to someother name and try again. beside why aren't you using switch case statement instead? Commented Feb 21, 2011 at 7:02
  • continue is not a valid identifier. it's a keyword in java. Commented Feb 21, 2011 at 7:04

4 Answers 4

3

Everything looks fairly good except

Boolean continue = true;

and

continue = false;

etc.

continue is a reserved word and may not be used as an identifier. Rename the variable to cont or something similar.

And by the way, I would recommend you to use boolean instead of Boolean in this application.

Also, I believe there is an error in your if-structures:

if (choice == 1)
    ...
if (choice == 2)
    ...
if (choice == 5)
    ...
else
    ...

The else branch will be taken in all cases when choice != 5 (that is, even when choice is 1, 2, 3, .... So perhaps you want to change it to

if (choice == 1)
    ...
else if (choice == 2)
    ...
else if (choice == 3)
    ...
else
    ...

Finally, you may also want to consider using a switch for the choice:

switch (choice) {

case 1:
    ...
    break;

case 2:
    ...
    break;

...

default:
    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

this now gives me error about "m1 is already defined" in the code and it doesn't compile.
Right, this is because break does not end the scope of a variable. Either you encapsulate the statements between case and break with { ... }, or, you don't redeclare m1 and instead use the m1 declared in previous case-blocks.
Unless you have "fall-through" (a case, without a break) the m1/m2 will not be initialized with any content at a "later case" so, yes, clear it (or overwrite it) if you want.
i fixed all I could. here is my problem now. i need to use a menu to print the matrices and I'm not quite sure how to do that. I keep getting errors when trying to print the matrices and it shows "cannot find symbol" at lines 153, 158, 163.
Sounds like you should post another question then. Post the link to the new question here, and I'll have a look at it :-)
1

I'm not sure if this may cause conflicts, but "continue" is also a statement within Java. Maybe, by changing the variable name of continue, your problem will be solved. The link attached refers to how continue is used as a statement within Java: http://download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html And what you could also do is make a switch() statement from the if() construction you have, but that's something of personal taste :)

1 Comment

you are not sure, but you wrote it correctly ;) (Avoid technical terms in your answer [statement, construction], it would be more understandable. Hope you overcome that in your upcoming posts :) )
1

To avoid getting the "m1 is already defined" error, write the switch like that (including the curly braces):

switch (choice) {

case 1: {
    ...
    break;
  }

case 2: {
    ...
    break;
  }

...

default: {
    ...
  }
}

Comments

0

If you have converted to 'switch' then declare the 'Matrix m1 = null;' just before the switch statement. And in the initialization of each 'case' just use 'm1 = new MatrixReader()'. Do the same for 'm2' variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.