Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Matrices (http://www.programmingforums.org/showthread.php?t=12999)

paulchwd Apr 15th, 2007 1:28 AM

Matrices
 
Hi,

i want to get the size of a matrix from a user at the command line and then prompt them to enter the values to be stored in the matrix,

i cant wrap my head around 2d arrays (programming them, can you help me pls.


thnx x10

titaniumdecoy Apr 15th, 2007 1:54 AM

If you know how to get user input, then creating a 2D array is simple enough:

:

int[][] matrix = new int[numRows][numCols];
You can access a specific element of the array as matrix[row][col].

If you need more help, your best bet would be to search the web for answers. A Google search for "2d array java" turns up a multitude of resources.

paulchwd Apr 15th, 2007 12:40 PM

im am using stringtokenizer to tokenize the user input (first row of data), but where do i put the while(st.hasMoreTokens()) loop?, here is my code:


:

StringTokenizer st = new StringTokenizer(data);
 
     
                    for (int i=0; i<=row-1; i++ )
                    {
                                System.out.println("I= " + i);

                            for(int j=0; j<=col-1; i++)
                            {

                                    System.out.println("J= " + j);
                                  addArray1[i][j]=Integer.parseInt(st.nextToken());
                                                       
                            }
                     
                    }




thnx x 10

titaniumdecoy Apr 15th, 2007 3:04 PM

Will you please stop rating all your threads 5 stars? The exclamation mark sign isn't necessary either. Your posts will be read.

If you expect the user to enter a specific number of tokens, a for loop would likely be preferable to a while loop.


All times are GMT -5. The time now is 4:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC