Results 1 to 6 of 6

Thread: Matrix class

  1. #1
    maz22 is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default Matrix class

    Hi!
    I have a program that should read in two matrix from console and add them together then write out the new matrix in matrix form. But that doesn't work. Please help.

    Main.java
    Java Code:
    package javaapplication8;
    
    import java.util.*;
    import java.io.*;
    
    public class Main {
    
        public static void main(String[] args) {
            try{
                new InputMenu();
            } catch (FileNotFoundException e){
                        System.err.println("Hibás paraméter");
                        return;
                    }
        }
    }
    InputMenu.java
    Java Code:
    package javaapplication8;
    
    import java.util.*;
    import java.io.*;
    
        public class InputMenu
        {
    
            int [][] s;
            int [][] m1;
            int [][] m2;
            int row;
    
    	    Scanner e = new Scanner(System.in);
                MatrixOperations f = new MatrixOperations();
    
                System.out.println("Give the number of the rows:\n");
                row=e.nextInt();
                f.getROW(row);
                int [][] m1 = new int[row][row];
                for(int i = 0; i < row; i++){
                for(int j = 0; j < row; j++){
                    m1[i][j] = e.nextInt();
                    //System.out.print(" "+ t1[i][j]);
                    }
                }
                
                System.out.print("Second matrix:\n");
                int [][] m2 = new int[row][row];
                for(int i = 0; i < row; i++){
                for(int j = 0; j < row; j++){
                    m2[i][j] = e.nextInt();
                   // System.out.print(" "+ t2[i][j]);
                    }
                }
                f.MatrixAddition(m1, m2);
                f.MatrixOut();
    }

    MatrixOperations.java
    Java Code:
    package javaapplication8;
    
    import java.util.*;
    
    public class MatrixOperations {
    	public int row;
    	public int[][] m1;
            public int[][] m2;
            public int[][] s;
    
        public int [][] MatrixAddition(int [][] m1, int [][] m2){
            for (int i=0; i<row; i++)
            {
                for(int j=0; j<row; j++)
                {
                    s = new int [i][j];
                    s[i][j]=(m1[i][j] + m2[i][j]);
                }
            }
            return s;
        }
    
        public void MatrixOut(){
            for (int i=0; i<row; i++)
            {
                for(int j=0; j<row; j++){
                
                    System.out.println(s[i][j]);
                }
            }
        }
    
        public int getROW(int row){
            return row;
        }
      }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Perhaps you could say what this code *does* do - does it compile? If so, what happens when you run it?

    A couple of things look a bit weird though:

    Java Code:
    } catch (FileNotFoundException e){

    FileNotFoundException? Why?

    Java Code:
    f.getROW(row);

    This line of code does absolutely nothing - which is not going to be good for the program. So what was it intended to do? And do you see why it does nothing?

  3. #3
    maz22 is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Actually the filenotfoundexception part is from a different code and I didn't post out the whole code, because I have problem with the matrix read in and addition. When I compile the code it gives me null.

  4. #4
    argus is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    hi,

    i'm definitely not sure, and never tried matrixes so far.But "row=e.nextInt()" seems to be initialized always to the same value.
    Maybe put in this in a loop:
    Java Code:
    while((row=e.hasNext())!=null){...}
    But i really don't know.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default

    maz22,

    Firstly, welcome to the forums.

    There are two problems I can spot with your code.

    Firstly, the value of row is not set so defaults to zero. Instead, assuming m1 and m2 have been checked to ensure they are the same size, you can call m1.length() and m1[].length().

    Secondly, 's' is redeclared within the for loop of the MatrixAddition method. This should be declared outside of the loop using the sizes determined in the first point.

    Regards.

  6. #6
    maz22 is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  2. scalability of matrix
    By ajay kumar in forum New To Java
    Replies: 1
    Last Post: 12-07-2009, 08:21 AM
  3. displaying 2D-Matrix
    By srinivasmallabathula in forum New To Java
    Replies: 2
    Last Post: 02-18-2009, 07:19 PM
  4. [SOLVED] Help with Matrix
    By Bernard Robitaille in forum JCreator
    Replies: 10
    Last Post: 02-14-2009, 02:19 AM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •