• Post Reply Bookmark Topic Watch Topic
  • New Topic

java Handling matrices  RSS feed

 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Can any one help me why my code is not printing the matrix and sum?
thank you!
Assignment
The Java-program Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.

Program to complete:



Example output


Type in the number of rows: 3
Type in the number of columns: 4
Type in the element 1 of the row 1: 1
Type in the element 2 of the row 1: 2
Type in the element 3 of the row 1: 3
Type in the element 4 of the row 1: 4
Type in the element 1 of the row 2: 5
Type in the element 2 of the row 2: 6
Type in the element 3 of the row 2: 7
Type in the element 4 of the row 2: 8
Type in the element 1 of the row 3: 9
Type in the element 2 of the row 3: 10
Type in the element 3 of the row 3: 11
Type in the element 4 of the row 3: 12

Matrix:
1 2 3 4
5 6 7 8
9 10 11 12

Sum of the elements of the matrix: 78
my code

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your printMatrix method you declare
int rows=0, columns=0;
So your loop does not print anything.
Use enhanced for loop or find a way to get length of an array.

The same happens in your sum method.
You declare
int sum=0;
And you print it.
So it prints: 0.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited your post because some of the lines were too long and the many blank lines make it harder to read.

An enhanced for loop will not work when you are trying to assign to the elements of an array.
In the read info method, you have a local variable sum which you are never using; you can delete all references to it.
Don't use \n (despite what it says in many books) unless somebody has told you they want an LF character.
Don't write System.out.print("\n");
Use System.out.println(); with empty ().
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thank you for the suggestions. As per the assignment my task is only to create the methods.
I am not supposed to touch the main method, that is what it makes hard for me. When I modify
int rows=0; int columns=0; to int rows, columns; it says I have to initialize. So generally I am trying
to create three methods which fits the main. Any idea will help thank you!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice is. Forget about int variables rows and columns declared in main method and sum inside of askInfo. They don't exist outside of those methods. What you did is: you created new variables with the same names and set their values to 0. That is of course wrong.

You don't need to calculate sum inside of askInfo. This is a job for countSum method.

What is a parameter your methods take? It is int[] array. So use that information only to calculate sum or print the array. Nothing else is needed.
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
So if I forget about the rows and columns, how would my
method know what to print?
for (int i = 0; i < ?; i++) {
Can you show me how you would do it for the first method for example
askInfo(int rows, int columns)? Remember you can't accept user input of rows
and columns inside the methods. Because the output would look different.
Thanks.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
daniel kidanee wrote:Hi,
So if I forget about the rows and columns, how would my
method know what to print?
for (int i = 0; i < ?; i++) {
Can you show me how you would do it for the first method for example
askInfo(int rows, int columns)? Remember you can't accept user input of rows
and columns inside the methods. Because the output would look different.
Thanks.

You can get length of array by writing matrix.length.
Or even better. You can use enhanced for loop, so you don't even neet to know the length of the array!
One important thing to remember. Java doesn't support matrices or multi-dimensional arrays.
In Java one can declare arrays of arrays.
So matrix.length will give you numer of rows and matrix[i].length will give you number of columns in i-th row (in case you decided not to use enhanced for loops).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!