• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

printing letters through for loop

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:



John.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, thanks for the explanation guys, appreciate it
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can convert between char and int values, like this:
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic