aspose file tools*
The moose likes Beginning Java and the fly likes Alphabet Pyramid (this one!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Alphabet Pyramid (this one!)" Watch "Alphabet Pyramid (this one!)" New topic
Author

Alphabet Pyramid (this one!)

David J. Gonzo
Greenhorn

Joined: Feb 15, 2005
Posts: 20
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");
}




}
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Do you get an error?
David J. Gonzo
Greenhorn

Joined: Feb 15, 2005
Posts: 20
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'.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David J. Gonzo
Greenhorn

Joined: Feb 15, 2005
Posts: 20
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

Joined: Feb 15, 2005
Posts: 20
>"...so the first 'z' value must be what?"

Sorry, I meant 'zz'.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 15, 2005
Posts: 20
...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

Joined: Aug 31, 2004
Posts: 11343

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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
David J. Gonzo
Greenhorn

Joined: Feb 15, 2005
Posts: 20
Thanks for all of your help



....so is there a solution, or do I need to not use an array?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 15, 2005
Posts: 20
Thank You so much everyone! A big thanks to

MARK WEBER,

............one day I will return the favor to newbies
David J. Gonzo
Greenhorn

Joined: Feb 15, 2005
Posts: 20
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 15, 2005
Posts: 20
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

Joined: Aug 31, 2004
Posts: 11343

"...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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Alphabet Pyramid (this one!)