Transferring User Input Into an Array
I want the user to input whats inside the matrix so I tell them "Enter Row 1 of Matrix 1: " Say for example they entered the length of the square matrix as 3, I would then want them to enter something like "1 2 3" as Row 1. With my current code I get:
333
333
333
I cant figure out what I should change so that its:
123
123
123
Thanks so much for any help. Heres the code so far:
import java.util.Scanner;
publicclass MatrixAddPro
{
publicstaticvoid main (String[] args)
{
int row1, col1;
Scanner scan =new Scanner(System.in);
System.out.println("Enter the length of the square matrix: ");
col1 = scan.nextInt();
int[][] a =newint[col1][col1];
System.out.println("Enter Row 1 of Matrix1: ");
while(scan.hasNext())
{
a[1][1] = scan.nextInt();
}
//for (int row = 0; row < a.length; row++)
//for (int col = 0; col < a[row].length; col++)
//a[col1][col1] = scan.nextInt();
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
System.out.print(a[1][1] +"\t");
System.out.println();
}
}
}
[2156 byte] By [
MacMurphya] at [2007-11-27 1:56:29]

As you have the rows and columns then what is the problem?
for (int row = 0; row < a.length; row++)
{
for (int col = 0; col < a[row].length; col++)
//ask user to enter value for Row and Col
System.out.print(a[row][col] + "\t");
System.out.println();
}
System.out.print(a[1][1] + "\t");
See how you have it hard coded to always display element at row 1, column 1. Even if you fix it as suggested in reply #1, I imagine your ouptut will be
0 0 0
0 3 0
0 0 0
because you have the same problem during the assigning of input phase.
you are right.But we are not supposed to fix the whole code. We are here to hint the user.Now he has to see where he was doing wrong.Now he can get the input from the user and then accordingly store it use it and show it.
//ask user to enter value for Row and Col
Thats my whole problem. Im lost for what I need to actually put there. I tried this but only got errors:
for (int row = 0; row < a.length; row++)
for (int col = 0; col < a[row].length; col++)
{System.out.println("Enter Row 1 of Matrix1: ");
a[row][col] = scan.nextInt();
System.out.print(a[row][col] + "\t");
System.out.println();
}
Btw I understand the [1][1]. I was just using that as a placeholder so the program would run while I tried to figure out how to correctly input it.
how have to see how many rows and cols are there lets say
rows=2
cols=3
your array = new Array[rows][cols];
for(int x=0;x<rows;x++){
for(int y=0;y<cols;y++){
system.out.println("Enter value for" + x+ " and Col" + y);
int value = System.in.get.................
your array[row][cols] = value;
}
}
//show the data entered by users
for(int x=0;x<rows;x++){
for(int y=0;y<cols;y++){
system.out.println( your array[row][cols]);
}
}
Try to debug where you are doing wrong.
Might be there is some thing wrong in my code as i am directly writing here not testing it in any editor.>
Somethings wrong, Im staring at it now but this is what I came up w/ :
import java.util.Scanner;
public class MatrixAddPro
{
public static void main (String[] args)
{
int input, row, col;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of the square matrix: ");
col = scan.nextInt();
row = col;
int[][] a = new int[row][col];
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.println("Enter Row 1 of Matrix1: ");
input = scan.nextInt();
a[row][col] = input;
}
}
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.print(a[row][col] + "\t");
System.out.println();
}
}
}
}
Its an ArrayIndexOutofBoundsException because of this line:a[row][col] = input;
a[row][col] = input; Thing very carefully. What are row and col? Do they change?. Even if you didn't get out of bounds, all your values would be stuffed into the same slot of the array. Also, since you have a nested loop you will be asking user to enter a row 9 times for a 3x3
Instead of
a[row][col] = input;
It should be
a[x][y] = input;
Right?
But with that I still have a problem. Output becomes:
Enter the length of the square matrix:
3
Enter Row 1 of Matrix1:
1 2 3
Enter Row 1 of Matrix1:
Enter Row 1 of Matrix1:
Enter Row 1 of Matrix1:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at MatrixAddPro.main(MatrixAddPro.java:30)
the problem is on a[row][cols] it should be a[x][y]
you have to take care of the loops
int input, row, col;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of the square matrix: ");
col = scan.nextInt();
row = col;
int[][] a = new int[row][col];
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.println("Enter Row="+(x+1)+" Col="+ (y+1) + "=> " );
input = scan.nextInt();
a[x][y] = input;
}
}
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.print(a[x][y] + "\t");
}
System.out.println();
}
import java.util.Scanner;
public class MatrixAddPro
{
public static void main (String[] args)
{
int input, row, col;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the length of the square matrix: ");
col = scan.nextInt();
row = col;
int[][] a = new int[row][col];
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.println("Enter Row"+(x+1)+" Col"+ (y+1));
input = scan.nextInt();
a[x][y] = input;
}
}
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{System.out.print(a[x][y] + "\t");
System.out.println();
}
}
}
}
OUTPUT:
Enter the length of the square matrix:
3
Enter Row1 Col1
1
Enter Row1 Col2
2
Enter Row1 Col3
3
Enter Row2 Col1
1
Enter Row2 Col2
2
Enter Row2 Col3
3
Enter Row3 Col1
1
Enter Row3 Col2
2
Enter Row3 Col3
3
1
2
3
1
2
3
1
2
3
Alright so now its not printing in rows...
Thanks for your patience/help guys.
Im an idiot. I got it. Sorry for posting such retarded stuff.
One last problem for this program. I am trying to find the product of two square matrices. I have this code:
//Finds the product of the two matrices.
for (int x3 = 0; x3 < row; x3++)
{
for (int y3 = 0; y3 < col; y3++)
for (int z = 0; z < 1; z++)
d[x3][y3] = a[x3][z] * b[z][y3] + a[x3][z + 1] * b[z + 1][y3] + a[x3][z + 2] * b[z + 2][y3] ;
}
I want z to keep being incremented as long as it makes sense. I only have it the way I have it now because it was a 3 columned and rowed matrix that I was testing. This wont work w/ 2 columned or 4 columned. Thanks for any help.
At the moment the two matrics products formula is not in my mind.
But as I remember there are few rules which you have to follow before multiplying the two matrics e.g two matrix can be only multiplied if rows of first are equals to the 2nd one.
And the resulted matrics will be ..... of that size.
Keeping all these things in mind start the coding.