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
Joined: Apr 11, 2006
Posts: 11
posted
0
Sorry, that should be 5 rows and 12 Columns starting at unicode char 65 through to 124.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
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
Joined: Apr 11, 2006
Posts: 11
posted
0
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.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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
Joined: Apr 11, 2006
Posts: 11
posted
0
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
Sheriff
Joined: Oct 13, 2005
Posts: 32675
4
posted
0
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.
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.