2

My professor asked us to generate this output:

A1 B2 C3 D4 E5

F6 G7 H8 I9 J10

K11 L12 M13 N14 O15

P16 Q17 R18 S19 T20

U21 V22 W23 X24 Y25

Z26

I got the correct output but he won't accept my code; he said I have to do it without using an array and using only 2 loops. I can't think of any solutions that can generate the same output. I am wondering if it is possible to make the same output with only 2 loops? I made my code like this but my professor said I have to revise it.

public class lettersAndNumbers {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] abc = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z", };

        int i = 0;
        while ( i < abc.length ) {

            int j = 1;
            while ( j <= 26 ) {

                int k = 1;
                while ( k <= 5 ) {

                    System.out.print(abc[i] + j + "\t");
                    j++;
                    i++;
                        k++;

                    if ( k == 6 ) {
                        System.out.println();
                    }

                }
                k = 1;
            }
        }
    }

}
4
  • I think your professor will be very happy to know that you've done it yourself. Commented Nov 1, 2012 at 16:25
  • 6
    +1 for attempting it yourself, and not just asking for us to do it all for you! Commented Nov 1, 2012 at 16:26
  • 1
    Two loops? You can do that in one loop if you know about the modulus operator... Commented Nov 1, 2012 at 16:29
  • You could do it in zero loops using println(). You should ask Prof to clarify what the requirements were. Commented Nov 1, 2012 at 16:36

6 Answers 6

5

You can loop on chars actually, which will make your code more readable and avoid using an array for your letters:

int count = 1;
for (char letter = 'A'; letter <= 'Z';) {
    for (int i = 1; i <= 5; ++i, ++letter, ++count) {
        System.out.print(letter);
        System.out.print(count + "\t");
        if (letter == 'Z')
            return;
    }
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why is it i <= 5 is the condition in the loop? i dont get it :\
@JaGb clear your looping concepts first. It seems you havent used for loop ever.
2

Here's a way to do it in one for-loop:

// 'A' starts at 65
int ascii_offset = 65;

// Loop 26 times and print the alphabet
for (int index = 1; index <= 26; index++) {

    // Convert an ascii number to a char
    char c = (char) (ascii_offset + index - 1);

    // Print the char, the index, then a space
    System.out.print("" + c + (index) + " ");

    // After 5 sets of these, print a newline
    if (index % 5 == 0) {
        System.out.println("\n");
    }
}

For further reading about ascii's and int to char conversion, here's a related discussion: Converting stream of int's to char's in java

Comments

2

My Java is really really rusty, but I think this is what you're looking for:

for(int i = 0; i < 26; i++) {
  System.out.printf("%c%d ", 'A' + i, i + 1);

  if (i % 5 == 0) {
    System.out.println();
  }
}

Comments

0

Well, don't feel bad. I would have done the same thing you did at first. However, you can do this with just the two loops and no array.

Have one loop simply iterate from 1 to 26. Then have another loop iterate from the ASCII value of capital A (65), through the ASCII value of capital Z (90). All you need to do now, is convert from the ASCII value to a string, and concatenate.

http://www.asciitable.com/

Comments

0

Any problem doing it in one loop?

public class AlphaOneLoop {
public static void main(String[] args) {
    for (int i = 65; i <= 90; i++) {
        System.out.print(new String(Character.toChars(i)) + (i - 65 + 1) + " ");

    }
}

}

1 Comment

You forgot the newlines after every fifth print :p +1 if you implement it using the modulo-operator.
-2

This oughta do it:

String[] abc = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", };

for(int i=0; i<abc.length; i++){
 System.out.println(abc[i] + (i+1) + "\t");
 if( i % 5 == 0 ){
  System.out.println();
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.