• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bringing the cursor back to the start line  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program to print a pattern that represents a single flower such that each such flower is printed in 'n' rows and 'c' columns, one in each grid.
The single flower pattern is as follows:
..O..
O.o.O
..O..

where:
O = Uppercase 'O'
o = Lowercase'o'
. = period

Sample test case:
For 2 rows and 5 columns:
the pattern should be this:

..O....O....O....O....O..
O.o.OO.o.OO.o.OO.o.OO.o.O
..O....O....O....O....O..
..O....O....O....O....O..
O.o.OO.o.OO.o.OO.o.OO.o.O
..O....O....O....O....O..

i.e a single flower per grid.
I am having problem in printing this pattern for each column, since I don't know how to bring the cursor back to the start position for each column.


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to bring the cursor back to the start position for each column

The newline character ("\n) will bring the cursor back to the first position of a line.  Using println()  will also do it
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I mean row and not column here. Is there any command to bring the cursor back to the previous line?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once a line has been output and the cursor moved to the next line, I don't know that there is a way to back up.

The program has to plan all of what goes on a line before displaying it.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a StringBuilder where you can insert at any position. If you want multiple lines, consider multiple StringBuilders or even an array.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an ugly Brute Force approach I used to solve this problem but since the maximum  value of  'row' input by the user was  only 20 according to this problem, I hope this solution is not that bad.

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a relationship between all the numbers in the if statement that could be used to reduce it to a simpler statement?
Look at using the modulus operator.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Norm. I simplified the if statement as per your suggestion.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!