• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Alphabet Pyramid (this one!)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I am trying to get the finishing touches on the following program...I think the problem is in the 'zz' 'for' statement. It is the statement that prints the right half of the pyramid.Thanks for any help. I apologize for the previous post, I am new here...
/*

Requirements:The assignment is due next Wednesday. Please submit your project to the assignment folder:
Have a program request the user to enter an uppercase letter. Once a letter is entered produce a pyramid pattern.
Example: IF "E" or "e" is entered the pyramid should look like:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA*/

import java.io.*;
import java.util.*;
import java.lang.*;



public class alphabetPyramid {

public static void main(String[] arguments) {

int userInput;//declare and initialize string //Variables and arrays
int i=0;
char[] alphabet = {'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'};



System.out.println("Enter a capital letter. Press enter when done. ");//screen output

try
{
userInput = System.in.read();//a bit of error checking, but I need a little more...
i = userInput - 65;


for(int x = 0; x <= i; x++)//conditionals that attempt to create pyramid, but the Y conditional does not work.....
{//*************************************************************************************
for(int y = (x - i); y < 1 ; y++)
{
System.out.print(" ");
}
for(int z = 0; z <= i; z++)
{
System.out.print(alphabet[z]);
}
for(int zz = (x - i); zz >= 0; zz--)
{
System.out.print(alphabet[zz]);
}
System.out.println();
}




}

catch(Exception e)
{
System.out.println("error");
}




}
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you get an error?
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No errors.

The shape that is supposed to prints in the console is:

....A
...ABA
..ABCBA
.ABCDCBA

minus the 'periods', I used periods just for the formating on the forum post. In this example the user typed in a 'D'.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The naming policy requires you to use a real-sounding first and last name, like, for example, "David Gonzales" or something similar. Please go back and try again. Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is your code -- reformatted with comments, but otherwise unchanged.

As you can see from the output, the number of lines are correct, and the leading spaces are correct -- so you're almost there.

The first problem is in your z loop, which I've commented, "FORWARD CHARS." You want this to differ with each line of output, but this code is printing the same thing every line. (Hint: You've defined this loop as a function of i. But does i differ from line to line?)

The next problem is in your zz loop, which I've commented, "BACKWARD CHARS." As you can see, this isn't doing anything until the last line of output. Ask yourself this: How many of these chars do you want on the first line? on the second line? etc.

Both of these are minor adjustments -- once you see the pattern.
[ February 15, 2005: Message edited by: marc weber ]
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the 'z' for loop going, but that 'zz' for loop is vexing me. I think I have tried justa bout everything my little brain can come up with...

I realize the first 'A', at the top must not have a character follow it, so
the first 'z' value must be what? If it is a zero, that will print an 'A'?
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>"...so the first 'z' value must be what?"

Sorry, I meant 'zz'.
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find that modularizing the code might make it easier to debug and test

Something like this



All I am saying is that you can test each of the functions seperately and figure out your problem faster
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David J. Gonzo:
... I realize the first 'A' at the top must not have a character follow it, so the first 'zz' value must be what? If it is a zero, that will print an 'A'? ...


Does the 'z' loop not print an 'A' at the top? Or in general, the "middle" char of each row?

Since the middle char is handled by the "forward order" loop (z), the "reverse order" loop (zz) should print one less char per row than the forward order loop.

So when row = __, the number of chars output by the zz loop is __?
[ February 16, 2005: Message edited by: marc weber ]
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...can it be done then?


or will I have to change the aphabet array to char[]alphabet = {'A'}
[ February 16, 2005: Message edited by: David J. Gonzo ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David J. Gonzo:
... for(int zz = (x + 1); zz >= -x ; zz--) ...


zz is used as an array index, so decrementing into negative territory is sure to generate an exception.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,

You can easily go back and edit your previous posts any time you wish. You do this by clicking the pencil-and-paper icon at the top of your message on the far right.

Layne
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of your help



....so is there a solution, or do I need to not use an array?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David J. Gonzo:
...so is there a solution, or do I need to not use an array?


To avoid negatives, just put your boolean test expression back the way it was, to "zz >= 0".

for(int zz = (x - i); zz >= 0; zz--) {...

Then to make this whole program work, just change one little thing in the way you initialize zz. For each line, the loop starts with zz at a certain point, then counts down to zero (inclusively) as it adds characters to the right side of the pyramid. So if you're on line x (which starts at zero), then how many characters do you want on the right?
[ February 16, 2005: Message edited by: marc weber ]
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You so much everyone! A big thanks to

MARK WEBER,

............one day I will return the favor to newbies
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I had it working and now it is not working as it did the other day. What gives? I started a post about this. Anyway here is the algorithm that worked, and now is not working. Any help appreciated.


 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David J. Gonzo:
... int zz = (x - i) + 2; ...


This will work only if 'D' is entered as input (which I'm guessing was the test character you used). But this will not work for general cases.

Note that the number of reverse-order characters is a function of x. The variable i has nothing to do with it. And when 'D' is input (i.e., when this works), i = 3...
[ February 21, 2005: Message edited by: marc weber ]
 
David J. Gonzo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the opening for statement and then used zz = (x + 1) as follows.
I have been having strange things happens so we shall see how long it works.



Thanks Mark
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...zz = x + 1..." will work because you've also changed the outer loop to redefine x: "...int x = -1; x < i..."

But you could leave that outer loop as "...int x = 0; x <= i..." and then just use "...zz = x - 1..." Personally, I think this makes more sense, because x represents the line number; and designating your lines as 0 through i seems more manageable than -1 through i-1.

One more detail: The program clearly asks for an uppercase letter, and this is all it's designed to handle. But the example in your initial post says, "If 'E' or 'e' is entered..." Does the assignment require that you accept either upper or lowercase? (Of course, a careful programmer would account for this anyway, knowing that users seldom follow directions. )
[ February 22, 2005: Message edited by: marc weber ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic