Please help : How to print a matrix from the console...
Hi..
Please help:
How to print a matrix on the console , but i want to take all the input from the console.. like...
if the matrix is of size...
mxn
where
m : row
n : column
and the all the elements of the matrix from the console it self... .
Please help...
Nested for loop asking for each value, after asking for m and n first...Before you ask: no, I am not giving you demo code.
What do you mean by input from the console?
Do you want to use a GUI that allows a user to enter the information or are you going to supply all of the information on the command line?
If the former, then I would urge you to look at that section of Sun's Java tutorial that covers building GUI's. If the latter then you have some thinking to do.
It is easy to recover the values the user enters at the command line. I am sure you are familiar with the following method signature;
public static void main(String[] args)
All of the values that the user enters on the commanbd line are passed to the main method in that array of String objects. All you need to do is iterate through them in order to recover whatever info the user entered. But this is where you have to make a decision. How is the user to enter that information? How will you know what values have to be stored into which locations on the matrix?
I want to enter the values from the command line...
I have written a code.. which is taking input for the row and the coulmn from the command line ...
and i tried displaying the matrix also... its is taking the default value 0.
but i want to take the matrix values from the command line.. what should i do...
import java.util.Scanner;
public class CreatingAMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter the number of rows: ");
Scanner scanner1 = new Scanner(System.in);
int m = scanner1.nextInt();
System.out.print("Enter the number of coulmns:");
Scanner scanner2 = new Scanner(System.in);
int n = scanner2.nextInt();
System.out.println("The size of the matrix is : " +m+" x "+n );
int[][] a = new int[100][100];
for(int i=0;i<=m;i++)
{
for(int j =0; j <= n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
my output is this...
output:
Enter the number of rows: 2
Enter the number of coulmns:3
The size of the matrix is : 2 x 3
0000
0000
0000
If you want to read the values from the command line, then you should actually do so...And really, if you don't know how to do that after already having the code to print an array and to read values, you should take a break and try to continue later.
> int[][] a = new int[100][100];
Well, shouldn't you do something with the variables m and n here?
> for(int i=0;i<=m;i++)
> {
>for(int j =0; j <= n; j++)
>{
> System.out.print(a[i][j]+"\t");
>}
>System.out.println();
> }
>
>}
If you want to fill your matrix, you'd better ask the user to enter some values in that double for-statement.
Thanks...
I am able to print the elements of the array but i am not able to assign those values to the array ....
How to do that...
I just did a little change in my code...
import java.util.Scanner;
public class CreatingAMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter the number of rows: ");
Scanner scanner1 = new Scanner(System.in);
int m = scanner1.nextInt();
System.out.print("Enter the number of coulmns:");
Scanner scanner2 = new Scanner(System.in);
int n = scanner2.nextInt();
System.out.println("The size of the matrix is : " +m+" x "+n );
int[][] a = new int[100][100];
for(int i=0;i<=m;i++)
{
for(int j =0; j <= n; j++)
{
Scanner scanner3 = new Scanner(System.in);
int o = scanner3.nextInt();
}
System.out.println();
}
for(int i=0;i<=m;i++)
{
for(int j =0; j <= n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
I am getting an output as this
output:
Enter the number of rows: 2
Enter the number of coulmns:1
The size of the matrix is : 2 x 1
1
2
1
2
3
5
00
00
00
how should i assig those input values to my array..
Please help
> int[][] a = new int[100][100];
The same comment as in my previous post applies here.
> for(int i=0;i<=m;i++)
> {
>for(int j =0; j <= n; j++)
>{
> Scanner scanner3 = new Scanner(System.in);
> int o = scanner3.nextInt();
>}
>System.out.println();
> }
You don't need to create a new Scanner object everytime: just use one in your entire application.
You're not storing that variable o anywhere.
> Please help
By posting here it is obvious you need help. Stop posting "Please help" after every follow-up.
And you haven't responded to my last post here:
http://forum.java.sun.com/thread.jspa?threadID=5116483
Good luck.
I tried but i couldn't figure it out what to do...i am new to java.io package.. so what should i do to make my code work ..
I am going to guess that all you are seeing is the zeros output? If that is the case then you need to look closely at the piece of code below;
for(int i=0;i<=m;i++)
{
for(int j =0; j <= n; j++)
{
Scanner scanner3 = new Scanner(System.in);
int o = scanner3.nextInt();
}
System.out.println();
}
I have cut it straight out of the example abd will only ask one question. When and where are you storing the values that you are reading in from the user into the array?
Think about placing another line after you read the next integer value from the scanner object that will store the value read in into the array. If you are stuck for a solution, look at the line that is used to output the value from the array in the second nested for loop.