• 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

for loop demo

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im trying to print 12 columns and 5 rows using a for loop (one inside the other). Have tried many different permutations but just cant get my head around it! Please help. This is where i have got to. There have been many other variations but without success!!
 
richard stockman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, that should be 5 rows and 12 Columns starting at unicode char 65 through to 124.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (val = 65; val <= 124; val += 12) //val += 12
System.out.println(" ");
for (x = 0; x < 12; x++)//0-based
System.out.print((val+x)+" ");
 
richard stockman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, thats sorted out the rows and columns but now instead of getting unicode characters I get the actual values even though I declared va1 as a char at the start of my code.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using J5.0, have you tried the printf method rather than print?
You could printf("%c ", val) for each character on your rows.

Ar you looking for this sort of printout?

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 [ \ ] ^ _ ` 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 { |

CR
 
richard stockman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, thats what I'm looking for, the printf method works fine but when I was using the code from my first post the characters printed out using val even though they were not in the columns and rows that I wanted, now when I change the code to the working version I get numbers instead of characters. Is this something to do with the print((val+x)+" ")?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think the char primitive type is a character? It is a number, and when you add x which is another number to it, val behaves as a number.
Only when you keep chars away from other kinds of value will they behave as characters.


The %c operator forces the value back to a character.

CR
[ April 24, 2006: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before anybody tells me off for wrong terminology . . . .

Your adding an int to your char causes the char implicitly to cast to an int, so it shows its true colours as a number.

The %c behaves as if there were a hidden cast back to a char; try it with int values.
You can probably get print((val+x)+" ") to print a character with a (char) cast.



CR
 
richard stockman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your help.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only too pleased to help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic