• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
  • Tim Holloway
  • Carey Brown
  • salvin francis

Printing spaces

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to make pyramid of 2,10,18 and 26 stars. i printed stars but i cannot get stars in shape of pyramid. need just code for spaces.

for (int i = 1; i<=4; i++) {
           for (int s =1; s<= rows-1; s++ ) {
           for (int j=1; j<=(8*i)-6; j++) {
Posts: 65365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use print(""); try print(" "); instead.
Use the standard form of a for loop please:
for (int i = 0; i < something; i++) ...

Don't start with 1 for the first element, but 0. Use < rather than <=. That way other programmers willl be more familiar with the form of your code and will be better able to help you.

And welcome to the Ranch
Posts: 13675
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for some of the terms used below.

A couple of things. First,

10 - 2 = 8
18 - 10 = 8
26 - 18 = 8

So, while the formula (8*i)-6 you used for the termination clause works, it seems more complicated than it needs to be.

This is just a guess but if the point of the exercise was to use the increment clause of the for-loop header to control how much you increment the loop variable by, then you missed the point. I believe another point of the problem was to have you use a initial value other than the usual 0. Sure, you used 1 as the initial value for your loop variable but I don't think that was the intent. Again, this is just conjecture on my part but for a beginner type exercise, it makes more sense to me if the intent was to have you start with the first value in your progression (2, 10, 18, 26).  Were you given an example related to this that maybe showed you how to use a for-loop to count by 2's or 5's? Like so:

Second, your logic for counting the number of spaces is wrong. It might help to visualize what you're trying to do, like so:


Each dot (.) above represents a space you need to print to get the proper pyramid shape you want. You need to calculate how many spaces you print for each corresponding number in the progression. Your current for-loop to print spaces won't do it.

Were you already taught that for-loops can have multiple loop variables and increments? Like so:

If you were already taught this, I think applying this concept is the whole point of this exercise.
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
professionally read, modify and write PDF files from Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!