# Easy Help...Printing out the alphabet.

• April 15th, 2012, 06:29 PM
Jsmooth
Easy Help...Printing out the alphabet.
I need to figure out a way to print out the alphabet using a 2d array using char[][] alpha = new char[10][5]; .

It would look like this.

A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
• April 15th, 2012, 06:38 PM
Norm
Re: Easy Help...Printing out the alphabet.
Can you explain why you need an array to print out what you have posted?
A simple loop like this will print it:
for (int i=0; i < 10; i++)
System.out.println("A B C D E");

You can define a 2D array like this:
String[][] twoD = {{"a"}, {"B","C"}};
• April 15th, 2012, 06:41 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
loops to store ‘A’ in the first row, 'B' in the second row, and so on, using the boundaries of [10][5] meaning 10 is the rows and 5 is the columns.

I am already using a 2d array of char [] [] alphabet = new char[10][5];
• April 15th, 2012, 06:43 PM
Norm
Re: Easy Help...Printing out the alphabet.
Quote:

store ‘A’ in the first row, 'B' in the second row
I assume that those letters go in the first column on each row. What goes in the other four columns on the row?
If there are 10 rows then the letters will go from "A" to "J".
• April 15th, 2012, 06:46 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Yes, I'm just looking for loops that will do that with the 2D array.
• April 15th, 2012, 06:56 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Quote:

Originally Posted by Norm
I assume that those letters go in the first column on each row. What goes in the other four columns on the row?
If there are 10 rows then the letters will go from "A" to "J".

Yeah that is how I want it. So that it looks like this
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
• April 15th, 2012, 07:01 PM
Norm
Re: Easy Help...Printing out the alphabet.
That is different than you said:
Quote:

'A’ in the first row, 'B' in the second row
That would be:
A
B
C
D

Rows go down, columns go across.

You can define a 2D array in a statement like this:
String[][] twoD = {{"a"}, {"B","C"}};
• April 15th, 2012, 07:10 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Quote:

Originally Posted by Norm
That is different than you said:
That would be:
A
B
C
D

Rows go down, columns go across.

You can define a 2D array in a statement like this:
String[][] twoD = {{"a"}, {"B","C"}};

I can not define the 2D array using that statement. I need to define it using loops...
This should be the outcome.
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
• April 15th, 2012, 07:11 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Quote:

Originally Posted by Norm
That is different than you said:
That would be:
A
B
C
D

Rows go down, columns go across.

You can define a 2D array in a statement like this:
String[][] twoD = {{"a"}, {"B","C"}};

For example,

for(int i = 0; i <alpha.length;i++){
for(int j = 0; j<alpha[0].length;j++) {
System.out.print(alpha[i][j] + " ");

}
System.out.println();
}
but this doesn't print out correctly what I need
• April 15th, 2012, 07:12 PM
Norm
Re: Easy Help...Printing out the alphabet.
What does it print out? What is wrong with what it prints out?
• April 15th, 2012, 07:15 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Quote:

Originally Posted by Norm
What does it print out? What is wrong with what it prints out?

Prints out the opposite of what I need haha
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E
F F F F F
G G G G G
H H H H H
I I I I I
J J J J J
• April 15th, 2012, 07:22 PM
Norm
Re: Easy Help...Printing out the alphabet.
How do the rows go in your array? Are they the first dimension indexed by i?

Try reversing i & j: alpha[j][i]
• April 15th, 2012, 07:24 PM
Jsmooth
Re: Easy Help...Printing out the alphabet.
Quote:

Originally Posted by Norm
How do the rows go in your array? Are they the first dimension indexed by i?

Try reversing i & j: alpha[j][i]

I've tried reversing them but it just gives me an error when I run it.
java.lang.ArrayIndexOutOfBoundsException: 5
at exam2.main(exam2.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
• April 15th, 2012, 07:28 PM
Norm
Re: Easy Help...Printing out the alphabet.
Your code is printing out the columns of each row in the array row by row. You need to change the contents of the array is you want to see something different.
Your array has As in all columns of the first row, Bs in all col of second row etc