Reputation: 29
Here is the basic code for what is going on. I have a already prompted for the input of the number of rows and columns, which works. The problem is once the program reaches "Enter the data..."
it prints it but completely ignores the call to Matrix
. Inside of Matrix
I prompt to enter a matrix using the scanner class as well. Also, I realize this has no end currently. Please help.
case 1:
System.out.println("Enter the data for the first matrix.");
//input needed
MatrixOne = Matrix(Rows, Columns);
System.out.println("+");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Plus = Add(MatrixOne, MatrixTwo, Rows, Columns);
Print(Rows, Columns, Plus);
break;
Here is the method:
public static int [][] Matrix(int Rows, int Columns)
{
int[][] NewMatrix = new int[Rows][Columns];
for(int i=0; i<Rows; i++)
{
for(int j=0; j<Columns; j++)
{
System.out.println("HIIIIIIII");
NewMatrix[i][j] = keyboard.nextInt ();
}
}
return NewMatrix;
}
Here is the output currently:
Please choose an operation: Addition(1) or Subtract(2)1
How many Rows would you like your matrix to have?
2
How many Columns would you like your matrix to have?
2
Enter the data for the first matrix.
+
Enter the data for the second matrix.
=
Please choose an operation: Addition(1) or Subtract(2)
This is what's above everything:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
RowColumns(Rows, Columns);
And this is the method:
public static void RowColumns(int Rows, int Columns)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns = keyboard.nextInt();
}
And main
:
public static void main(String[] args)
{
String End;
int operation;
int Rows = 0;
int Columns = 0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
RowColumns(Rows, Columns);
/////////////////READS INPUT AND DETERMINES TO ADD OR SUBTRACT//////////////////////////////////
switch(operation)//Reads the value of the variable Operation. If Operation equals 1, the program will add the matrices,...
//if Operation equals 2, the program will subtract the matrices, and if the Operation equals anything other than 1 or 2, the...
//user will be prompted to enter either 1 or 2 again.
{
case 1:
System.out.println("Enter the data for the first matrix.");
//input needed
MatrixOne = Matrix(Rows, Columns);
System.out.println("+");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Plus = Add(MatrixOne, MatrixTwo, Rows, Columns);
Print(Rows, Columns, Plus);
break;
case 2:
System.out.println("Enter the data for the first matrix.");
MatrixOne = Matrix(Rows, Columns);
System.out.println("-");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Minus = Subtract(MatrixOne,MatrixTwo, Rows, Columns);
Print(Rows, Columns, Minus);
break;
default: System.out.println("Please enter 1 to add the matrices or 2 to subtract them.");
}//End of Switch
}while(operation != 1 || operation != 2);
}//End of main
Upvotes: 0
Views: 101
Reputation: 798
Java is pass by value
.
In RowColumns
function, you are passing
two integers by value
which means that, only their values are passed to the function, not the references. Because of that, actual references of those variables will not be affected. Therefore, after the RowColumns(Rows, Columns)
call, Rows
and Columns
variables will remain same with their initial values (0, 0).
If you want to change the values of the variables inside of a function, then you should return them.
To do this, you can define a container for these two values:
public class Size {
public int Rows;
public int Columns;
}
Then you can initialize and use it as;
public static void Main(String[] args) {
...
Size size = new Size();
...
size = RowColumns(size);
...
}
public Size RowColumns(Size size)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
size.Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
size.Columns = keyboard.nextInt();
return size;
}
Upvotes: 1
Reputation: 403
Your code actually is calling the Matrix
method perfectly fine, the issue is with your RowColumns
method. Your RowColumns
method isn't actually setting the variables you think it is, so when your Matrix
method is called the Row and Columns variables passed to it are zero.
Let me break down what exactly is happening. In java integers are passed by value, not by reference. This means that when you call RowColumns(Row, Columns);
whats really happening is the actual value of Row and Columns are taken and sent off to the RowColumns method, not the variables themselves. Effectively this is happening RowColumns(0, 0);
Despite the parameters in your RowColumns
method having the same name they are not the same variable. Here is another way to look at your code.
int Rows1=0;
int Columns1=0;
RowColumns(Rows1, Columns1);
and your RowColumns
method
public static void RowColumns(int Rows2, int Columns2)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows2 = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns2 = keyboard.nextInt();
}
When RowColumns
gets called Rows2
is set equal to Rows1
and Columns2
is set equal to Columns1
, notice how your RowColumns
doesn't actually effect Rows1
and Columns1
. In your code you have confused yourself by naming the parameters of RowColumns
the same as the variables being passed to it, which means that while it looks like they are the same, they are in fact completely different.
One solution to this is to simply do away with the RowColumns
method if you don't need it else where like this:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns = keyboard.nextInt();
This would accomplish the same thing but this time you would be using the Rows
and Columns
variables you want to.
If you want to keep the user input to its own method the most straight forward solution would be to create two separate methods, one to get the rows and one to get the columns, then have those methods return the value given by the user. Something like this:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
Rows = getRows();
Columns = getColumns();
and the two methods would look something like this:
public static int getRows()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
return keyboard.nextInt();
}
public static int getColumns()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Columns would you like your matrix to have?");
return keyboard.nextInt();
}
There are all kinds of ways to solve this problem, I just gave the two most straight forward ones. Hope this sufficiently answers your question!
Upvotes: 0
Reputation: 26
In the Matrix method, it won't the enter the loop if the Rows or Columns is zero; so maybe they may have a value of zero.
Upvotes: 0