43

"Auto increment" alphabet in Java - is this possible? From A to Z without a third-party library?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dacto
  • 2,901
  • 9
  • 45
  • 54

9 Answers9

117

Yes, you can do it like this:

for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
    System.out.println(alphabet);
}

It is also possible with typecasting:

for (int i = 65; i <= 90; i++) {
    System.out.println((char)i);
}
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Richie
  • 9,006
  • 5
  • 25
  • 38
  • 4
    omg haha i never even thought about this, i actually thought there was a magic function that did it lol. this is much better imo tho. – Dacto Jan 12 '10 at 07:01
18

Yes, like this:

for (int i = 0; i < 26; i++)
{
    char upper = (char) ('A' + i);
    char lower = (char) ('a' + i);
    ...
}
Community
  • 1
  • 1
Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
9
for (char c = 'A'; c <= 'Z'; c++) {
  ...
}
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • Note that this will only do upper case. If you want lower case too you need two loops, or you can do two steps in each iteration and add the distance between 'A' ans 'a' to c each time. – captncraig Jan 12 '10 at 06:57
4

You are looking for something like this:

    for( int i = 'a'; i < 'z'; i++ )
        System.out.println((char)i); // Cast int to char
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michel Gokan Khan
  • 2,525
  • 3
  • 30
  • 54
4

Mandatory Java 8 solution:

IntStream.rangeClosed('A', 'Z')
         .mapToObj(c -> "" + (char) c)
         .forEach(System.out::println);
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
2
for (char c = 'a'; c <= 'z'; c++)
  //whatever
sdornan
  • 2,805
  • 2
  • 15
  • 7
2

This is my solutions, just a little more complicated than other examples above, but extendible for other iterations (used pattern iterator):

class Alphabet implements Iterable<String>{

    private char start;
    private char end;

    public Alphabet(char start, char end) {
        this.start=start;
        this.end=end;
    }

    @Override
    public Iterator<String> iterator() {
        return new AlphabetIterator(start, end);
    }

    class AlphabetIterator implements Iterator<String>{

        private String current;
        private String end;

        private AlphabetIterator(char start, char end) {
            this.current=String.valueOf(--start);
            this.end=String.valueOf(end);
        }   

        @Override
        public boolean hasNext() {
            return (current.charAt(0) < end.charAt(0));
        }

        @Override
        public String next() {
            char nextChar = current.charAt(0);
            return this.current=String.valueOf(++nextChar);
        }
    }

    public static void main (String[] arg){

        for (String str:new Alphabet('B', 'Y')){
            System.out.print(str+" ");
        }
    }
}

Output: B C D E F G H I J K L M N O P Q R S T U V W X Y

1
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
    System.out.println(alphabet);
}

Use this for lowercase alphabets.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raja
  • 2,393
  • 2
  • 22
  • 25
0

Here is a piece of code to see what really is going on (or at least the print out :P):

for(int i = 0; i < 26; i++)
{
    System.out.println((char)('A' + i) + ":" + ('A' + i) + " : " + (char)('a' + i) + ":" + ('z' + i));
}