2

So I'm supposed to write out the upper and lowercase Greek alphabet for an assignment, but I feel like there's a way in Java that I can use a loop, and casting ints to chars (or something along those lines) in order to do this most efficiently. So i tried this:

public class GreekAlphabet{

public static void main(String[] args){
    char output;

    for (int i=0;i<1000;i++){

        output= (char) i;

        System.out.println(output);
    }
}

}

but it's not printing out greek letters, and after a certain amount of chars (256 I'm guessing), it's all question marks. Is there anyway to get an extended char set, or something like that?

user1418214
  • 59
  • 2
  • 9
  • http://unicode.org/charts/ and http://en.wikipedia.org/wiki/Greek_alphabet#Greek_in_Unicode – assylias Sep 26 '12 at 22:17
  • 3
    The console's charset most likely doesn't support the characters your are printing. Also, you can just iterate through chars instead of ints in your for loop, removing the necessity to cast ints. – FThompson Sep 26 '12 at 22:17
  • So am I just going to have to bite the bullet on this one and do it by brute force? – user1418214 Sep 26 '12 at 22:52
  • 1
    See http://stackoverflow.com/questions/12568077/cant-print-hindi-characters/12568354#12568354. Same question - what console are you using (Eclipse debugger console, windows cmd.exe, linux shell ...). Until you configure your console to support UTF-8 you will continue to see question marks regardless of whether you use a loop or manually enter the unicode values into your code. – Guido Simone Sep 26 '12 at 23:11

1 Answers1

0
for (char c = 'Α'; c <= 'Ω'; c++) {
    System.out.println(c);
    if (c == 'Ρ') c++;
}

Note that those are the Greek letters alpha and rho, not English A and P. Also, for some reason the Greek alphabet is not completely contiguous in Unicode; there's another character after rho, so that's why it goes up by two after that letter.

Joe K
  • 18,204
  • 2
  • 36
  • 58