I want to make a program to accept strings and make a pyramid of strings, but I cant make the right loop, I have the logic:
But this seems to make only half- pyramid, can somebody help me with the right loop

import java.util.Scanner;
public class Stairs
{
    public void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string");
        String a = sc.nextLine();
        int x, y;
       for (x = 0; x < a.length(); x++)
       {
            for (y = 0; y <= x; y++)
            {
                char c = a.charAt(y);
                System.out.print(c);
            }
            System.out.println();
       }
   }
}


        for (y = 0; y <= x; y++)
        {
            char c = a.charAt(y);
            System.out.print(c);
        }
        System.out.println();
   }

Recommended Answers

All 3 Replies

can you be a bit more clear on what it is you try to do, give for instance an example of 'input= ... , expected output = ... '

eg: input = java, then j on first level, aaa on second level, vvvvv on third level, aaaaaaa on fourth level (as you can see how i did it in the notepad0

before starting to print, calculate the length of the string you entered, then you know the width the 'biggest' String. make sure your first has half of that length in spaces before you print the 'j'
calculate about the same way for the other rows

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.