Learning Arrays  
Author Message
Jo Campbell





PostPosted: 2004-11-28 9:30:00 Top

java-programmer, Learning Arrays Hi, I am just learning arrays and I am trying to get this 2D array to print
out like this:

0 1 2 3 4 5 6
0 1 2 3 4 5 6
0 1 2 3 4 5 6
0 1 2 3 4 5 6
0 1 2 3 4 5 6

but insteads it prints out like this: 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4
5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6

I can get it to work with system.out.println but I need to use a
JOptionPane.showMessageDialog box as I have attempted to do. Any ideas?
(my code is below) Thanks!!!!!!!!!!

import javax.swing.*;

public class array5by7{

public static void main (String args[]){

String outputString = "";

int table[][] = new int [5][7];

for (int row=0; row<table.length; row++)
{
for (int col = 0; col <table[row].length; col++)
table[row][col] = 0 + col;
}



for (int row=0; row<table.length; row++)
{
for (int col = 0; col<table[row].length; col++)
outputString += (table[row][col] + " ");
}

JOptionPane.showMessageDialog (null, outputString + "\n");


}


}



 
Andrew Thompson





PostPosted: 2004-11-28 9:47:00 Top

java-programmer >> Learning Arrays On Sun, 28 Nov 2004 01:29:49 GMT, Jo Campbell wrote:

> Hi, I am just learning arrays and I am trying to get this 2D array to print
> out like this:
>
> 0 1 2 3 4 5 6
> 0 1 2 3 4 5 6

<sscce>
import javax.swing.*;

public class array5by7{

public static void main (String args[]){

String outputString = "";

int table[][] = new int [5][7];

for (int row=0; row<table.length; row++)
{
for (int col = 0; col <table[row].length; col++)
table[row][col] = 0 + col;
}



for (int row=0; row<table.length; row++)
{
for (int col = 0; col<table[row].length; col++) {
outputString += (table[row][col] + " ");
}
outputString += "\n";
}
JOptionPane.showMessageDialog (null, outputString + "\n");
}
}
</sscce>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
ExGuardianReader





PostPosted: 2004-11-28 16:00:00 Top

java-programmer >> Learning Arrays Jo Campbell wrote:
> Hi, I am just learning arrays and I am trying to get this 2D array to print
> out like this:
>
> 0 1 2 3 4 5 6
> 0 1 2 3 4 5 6
> 0 1 2 3 4 5 6
> 0 1 2 3 4 5 6
> 0 1 2 3 4 5 6
>
> but insteads it prints out like this: 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4
> 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6
>
> I can get it to work with system.out.println but I need to use a
> JOptionPane.showMessageDialog box as I have attempted to do. Any ideas?
> (my code is below) Thanks!!!!!!!!!!
>
> import javax.swing.*;
>
> public class array5by7{
>
> public static void main (String args[]){
>
> String outputString = "";
>
> int table[][] = new int [5][7];
>
> for (int row=0; row<table.length; row++)
> {
> for (int col = 0; col <table[row].length; col++)
> table[row][col] = 0 + col;
> }
>
>
>
> for (int row=0; row<table.length; row++)
> {
> for (int col = 0; col<table[row].length; col++)
> outputString += (table[row][col] + " ");
outputString += "\n";
> }
>
> JOptionPane.showMessageDialog (null, outputString + "\n");
>
>
> }
>
>
> }
>
>
>

 
 
SMC





PostPosted: 2004-11-29 14:26:00 Top

java-programmer >> Learning Arrays On Sun, 28 Nov 2004 18:59:49 +1100, ExGuardianReader wrote:

> Jo Campbell wrote:
<snip>
> outputString += "\n";
<snip>

More portable would be: System.getProperty("line.separator") instead of
hardcoding "\n", but hey, we all do it.

--
Sean

Happiness is walking the length of a long fairway with a putter in hand
- Greg Norman