Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Easy Help...Printing out the alphabet.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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"}};
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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];

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Easy Help...Printing out the alphabet.

    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".
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Yes, I'm just looking for loops that will do that with the 2D array.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Quote Originally Posted by Norm View Post
    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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Easy Help...Printing out the alphabet.

    That is different than you said:
    '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"}};
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Quote Originally Posted by Norm View Post
    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

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Quote Originally Posted by Norm View Post
    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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Easy Help...Printing out the alphabet.

    What does it print out? What is wrong with what it prints out?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Quote Originally Posted by Norm View Post
    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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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]
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy Help...Printing out the alphabet.

    Quote Originally Posted by Norm View Post
    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)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  2. This will be an easy one.
    By Pedroski in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2011, 12:53 AM
  3. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM
  4. Listing the alphabet in lower and uppercase
    By Nemphiz in forum Object Oriented Programming
    Replies: 2
    Last Post: May 25th, 2010, 05:25 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM