Cyart
asked on
Formating 2 dimensional array on screen
Hello,
I am after a suggestion to the above, I want to display the elements of a 2d array on the screen. I have been using a message box to display the results but as there are various lengths of either strings or integers, the columns do not line up. I have used the \t for positioning the column, but it is not ideal, can anyone suggest anything. I would ideally like to stick with a message box for outputting the array
cheers
I am after a suggestion to the above, I want to display the elements of a 2d array on the screen. I have been using a message box to display the results but as there are various lengths of either strings or integers, the columns do not line up. I have used the \t for positioning the column, but it is not ideal, can anyone suggest anything. I would ideally like to stick with a message box for outputting the array
cheers
ASKER
Every word could differ in the array, is there no easier way?
Why dont you just use JTable?
ASKER
just found something on jtable, thanks for the info will try yours as well
ASKER
Hi,
After some code as an example so I can execute from the static main function.
The current code is as follows I want to use the second for loop for the Jtable
public static void main( String args[] )
{
int array1[][] = {{56,6,3,0}, //Two dimensional array declaration
{4,-1,3,9},
{-10,0,12,8},
{3,7,5,13}} ;
//Assign the values returned from the method order to the returnedArray variable array
returnedArray = order(array1) ;
String output = "" ; //variable used for displaying the original and new array values to the user
output = "Array prior to sorting:-\n" ; //Assign text to the output variable
System.out.println() ;
//For loop used for populating the output variable with values from the elements of the array1 array
for(int x = 0; x < array1.length ; x ++ )
{
for( int y = 0 ; y < array1.length ; y++ )
{
output += array1[x][y] ;
}
output+="\n" ;
}
output += "\n\nArray sorted in ascending order:-\n" ;
//For loop used for populating the output variable with values from the elements of the returnedArray array
for(int x = 0; x < returnedArray.length ; x ++ )
{
for( int y = 0 ; y < returnedArray.length ; y++ )
{
output += returnedArray[x][y] ;
}
output+="\n" ;
}
//Display the results to the user via a message box
JOptionPane.showMessageDia log(null, output , "Exercise 5" , JOptionPane.PLAIN_MESSAGE) ;
System.exit (0);
}
After some code as an example so I can execute from the static main function.
The current code is as follows I want to use the second for loop for the Jtable
public static void main( String args[] )
{
int array1[][] = {{56,6,3,0}, //Two dimensional array declaration
{4,-1,3,9},
{-10,0,12,8},
{3,7,5,13}} ;
//Assign the values returned from the method order to the returnedArray variable array
returnedArray = order(array1) ;
String output = "" ; //variable used for displaying the original and new array values to the user
output = "Array prior to sorting:-\n" ; //Assign text to the output variable
System.out.println() ;
//For loop used for populating the output variable with values from the elements of the array1 array
for(int x = 0; x < array1.length ; x ++ )
{
for( int y = 0 ; y < array1.length ; y++ )
{
output += array1[x][y] ;
}
output+="\n" ;
}
output += "\n\nArray sorted in ascending order:-\n" ;
//For loop used for populating the output variable with values from the elements of the returnedArray array
for(int x = 0; x < returnedArray.length ; x ++ )
{
for( int y = 0 ; y < returnedArray.length ; y++ )
{
output += returnedArray[x][y] ;
}
output+="\n" ;
}
//Display the results to the user via a message box
JOptionPane.showMessageDia
System.exit (0);
}
JTable table = new JTable(x, y);
for (int x = 0; x < returnedArray.length; x++) {
for (int y = 0; y < returnedArray[x].length; y++) {
table.setValueAt(new Integer(returnedArray[x][y
}
}
JDialog dialog = new JDialog(null, "Exercise 5", true);
dialog.getContentPane().ad
dialog.setVisible(true);
that should replace your second loop, and your call for JOptionPane.showMessageDia
showMessageDialog is just a convience tool - This way you create your own dialog and put a JTable in it.
Btw, you'll probably need to add some buttons (Like OK) as well.
AviadBD.
ASKER
Hi,
Code not quite correct getting errors with table.setValueAt and JDialog dialog
Code not quite correct getting errors with table.setValueAt and JDialog dialog
Umm.... Thats right. Oops. :)
the setValueAt should be: table.setValueAt(new Integer(returnedArray[x][y
What errors with the JDialog, though?
ASKER
JDialog sorted removed the text within the brackets for the moment, but there still is a problem with the following line of code
table.setValueAt(new Integer(returnedArray[x][y ], x,y));
Cannot resolve sysmbol constructor Integer (int,int,int)
table.setValueAt(new Integer(returnedArray[x][y
Cannot resolve sysmbol constructor Integer (int,int,int)
should be:
table.setValueAt( new Integer( returnedArray[x][y] ), x, y) );
table.setValueAt( new Integer( returnedArray[x][y] ), x, y) );
Agree with JugglerW i am.
I dont understand how I made the same typo twice. :)
AviadBD.
ASKER
code is ok now but the dialog displays but with nothing in it. I know that there is data in the array as I can use the JOptionpane to show the contents of the array
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Venabili: Sorry. Doesn't realize it's homework.
aviadbd: Do you mean the code I've posted and Venabili has deleted? If so you'll have to wait until I'm at home this evening.
aviadbd: Do you mean the code I've posted and Venabili has deleted? If so you'll have to wait until I'm at home this evening.
JugglerW,
I think aviadbd is talking to the Asker :)
And it's ok.. just be more carefull next time:)
Venabili
I think aviadbd is talking to the Asker :)
And it's ok.. just be more carefull next time:)
Venabili
ASKER
Hello,
Sorry for causing any problems I wasn't trying to break any rules. I have the program working with the original code using the for loop and a JTextArea, which is fine for my question. However, after trying the JTable submission in the book "Java the complete reference" I found that it worked I thought all was well, but after trying to use an array that was returned from a function I found that it didn't work, this I tried after aviadbd had posted his post saying why not try a JTable.
Again sorry
Sorry for causing any problems I wasn't trying to break any rules. I have the program working with the original code using the for loop and a JTextArea, which is fine for my question. However, after trying the JTable submission in the book "Java the complete reference" I found that it worked I thought all was well, but after trying to use an array that was returned from a function I found that it didn't work, this I tried after aviadbd had posted his post saying why not try a JTable.
Again sorry
Do you need any further help?
Aviad.
ASKER
Hello,
Its not working, but it is not a problem i'll have a look when I have got a bit more time
Thanks for all your help
Its not working, but it is not a problem i'll have a look when I have got a bit more time
Thanks for all your help
First determine the max length of every column you want to print, second print out the columns and fill each line with enough spaces to reach the longest entry plus 2 or more spaces for a gap between columns.