File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes printing letters through for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printing letters through for loop" Watch "printing letters through for loop" New topic

printing letters through for loop

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36

I'm having a problem printing letters using a for loop..

I have this code:

The problem is what if I want to print out all the letters to Z? Obviously, putting all letters inside the letter array isn't really the best idea so I was wondering if something similar to this is possible:

I'm getting invalid unicode escape error on that code .. Thanks!
sandeep raj

Joined: Mar 06, 2009
Posts: 10
if you are looking for a technique to print out the letters then you could probably do this as well

I believe you are getting the invalid unicode error cause at compile time the compiler is looking for 4 digits after \u and since you only 3 it gives the error
John de Michele

Joined: Mar 09, 2009
Posts: 600
Actually, your first code snippet has a bug. Line 4 should probably read:

I think Unicode escapes need to be two characters (i.e., \uNN) or four characters (\uNNNN). In any case, your second snippet won't work the way you think it will. You'll just print out whatever the Unicode character is, plus the number, six times. The plus just concatenates them in the output.

To get from A to Z, try this:

Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

Remember the definition of a character constant..

A character written between single quotes representing an "integer value" equal to the numerical value of the character in the machine's character set(Unicode,ASCII etc.)

So, if you try to print the integer equivalent of any character(i.e whatever unicode uses to represents a symbol), as a character, you will get a character constant.

For e.g: Integer equivalent of A,B and C in Unicode are 65,66 and 67. So, the following

will print A,B and C.

Using this idea you can print any character in the Unicode character set.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Ah, thanks for the explanation guys, appreciate it
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
You can convert between char and int values, like this:
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
I agree. Here's the link:
subject: printing letters through for loop
jQuery in Action, 3rd edition