Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

for loop demo  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 64496
225
  • Mark post as helpful
  • send pies
  • 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
  • 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: 64496
225
  • Mark post as helpful
  • send pies
  • 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: 64496
225
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thankyou for your help.
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only too pleased to help.
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!