aspose file tools*
The moose likes Beginning Java and the fly likes for loop demo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop demo" Watch "for loop demo" New topic
Author

for loop demo

richard stockman
Greenhorn

Joined: Apr 11, 2006
Posts: 11
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
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
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
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: 36578
    
  16
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
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: 36578
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

Joined: Apr 11, 2006
Posts: 11
Thankyou for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Only too pleased to help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop demo
 
Similar Threads
oddSum
Comparable..
Could I modify an element using enhanced for loop
Multi Dimension Array Storing
stars