Results 1 to 6 of 6
Thread: Matrix class
 10142010, 08:41 AM #1Member
 Join Date
 Oct 2010
 Posts
 6
 Rep Power
 0
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; } } }
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; } }
 10142010, 08:47 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,709
 Rep Power
 13
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?
 10142010, 08:53 AM #3Member
 Join Date
 Oct 2010
 Posts
 6
 Rep Power
 0
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.
 10142010, 03:17 PM #4Member
 Join Date
 Oct 2010
 Posts
 11
 Rep Power
 0
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){...}
 10142010, 03:39 PM #5Senior Member
 Join Date
 Oct 2010
 Posts
 393
 Rep Power
 6
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.
 10152010, 06:53 AM #6Member
 Join Date
 Oct 2010
 Posts
 6
 Rep Power
 0
Similar Threads

help in matrix
By Engineer in forum New To JavaReplies: 7Last Post: 10062010, 01:26 PM 
scalability of matrix
By ajay kumar in forum New To JavaReplies: 1Last Post: 12072009, 09:21 AM 
displaying 2DMatrix
By srinivasmallabathula in forum New To JavaReplies: 2Last Post: 02182009, 08:19 PM 
[SOLVED] Help with Matrix
By Bernard Robitaille in forum JCreatorReplies: 10Last Post: 02142009, 03:19 AM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks