RfromP
asked on
Letter Pyramid
Hello,
I'm currently taking my first course in programming and I'm stuck on one of my Java assignments.
Let me caveat by saying I'm not looking for someone to do my homework but I need help.
My assignment is to:
A. Prompt a single character (Case-insensitive) from 'A' through 'Z'
(from 'a' through 'z').
B. Get a single character (Case-insensitive) from 'A' through 'Z'
(from 'A' through 'Z').
C. Produce output (uppercase) in the shape of a pyramid composed
of the letters up to and including the letter that is input.
D. Display output (uppercase) in the shape of a pyramid composed
of the letters up to and including the letter that is input.
So far I've been able to do A & B just fine. C & D, I've done somewhat but not exactly.
Here's my code:
Here's my output:
The output should look like this (depending on what letter was input, in this case 'e'):
I've been able to do what I've done on my own so far but it has not been very intuitive to me. I just can't figure it out. It seems to me that I'm somehow going to have to tell Java to subtract the amount of letters from the letter that was input and then display the pyramid up to and including the letter that was input but I have no idea of how to do that. So far in the course, we've only covered the Java basics up to looping. Seems like this assignment is for a more advanced level.
I'm an adult student in a distance education class and this is my first foray into programming so any help is appreciated.
I'm currently taking my first course in programming and I'm stuck on one of my Java assignments.
Let me caveat by saying I'm not looking for someone to do my homework but I need help.
My assignment is to:
A. Prompt a single character (Case-insensitive) from 'A' through 'Z'
(from 'a' through 'z').
B. Get a single character (Case-insensitive) from 'A' through 'Z'
(from 'A' through 'Z').
C. Produce output (uppercase) in the shape of a pyramid composed
of the letters up to and including the letter that is input.
D. Display output (uppercase) in the shape of a pyramid composed
of the letters up to and including the letter that is input.
So far I've been able to do A & B just fine. C & D, I've done somewhat but not exactly.
Here's my code:
import java.util.*;// For using Scanner class for input
public class LetterPyramid
{
static Scanner cin = new Scanner(System.in);
public static void main(String[] args)
{
char userInput = ' ';
int line = 0, space = 0, letter = 0;
System.out.print("Enter a single letter: ");
userInput = cin.next().charAt(0);
if (userInput >= 'A' && userInput <= 'Z' || userInput >= 'a' && userInput <= 'z')
{
userInput = Character.toUpperCase(userInput);
for(line = 1; line <= 26; line++)
{
for(space = 26; space > line; space--)
{
System.out.print(' ');// Displays one space
}
for(letter = 1; letter < line; letter++)
{
System.out.print(userInput);
}
for(letter = line; letter >= 1; letter--)
{
System.out.print(userInput);
}
System.out.println();
}
}
else
{
System.out.println("Error: Invalid letter " + userInput);
}
}
}
Here's my output:
Enter a single letter: a
A
AAA
AAAAA
AAAAAAA
AAAAAAAAA
AAAAAAAAAAA
AAAAAAAAAAAAA
AAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Enter a single letter: z
Z
ZZZ
ZZZZZ
ZZZZZZZ
ZZZZZZZZZ
ZZZZZZZZZZZ
ZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
The output should look like this (depending on what letter was input, in this case 'e'):
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
I've been able to do what I've done on my own so far but it has not been very intuitive to me. I just can't figure it out. It seems to me that I'm somehow going to have to tell Java to subtract the amount of letters from the letter that was input and then display the pyramid up to and including the letter that was input but I have no idea of how to do that. So far in the course, we've only covered the Java basics up to looping. Seems like this assignment is for a more advanced level.
I'm an adult student in a distance education class and this is my first foray into programming so any help is appreciated.
I thiink it would be easier to make a string array of the alphabet, and get the index in it of the letter you want. You could do ASCII decimal number calculations, but I bet your teacher is looking more for how to loop based on a string array.
So something like
String[] alphlist = {"A","B","C"}; // you get the idea
Then loop through alphlist until you hit the letter that equals the input. If you have to handle lower case vs upper case -- that is, if "e" is the input and you have to find "E" then you can make 2 string arrays, or you could use the String method equalsIgnoreCase .
Once you find the index in the array of the input letter, you know the strudcture of what you will be producing. Use the same alphabet string array to print out the pyramid lines.
So something like
String[] alphlist = {"A","B","C"}; // you get the idea
Then loop through alphlist until you hit the letter that equals the input. If you have to handle lower case vs upper case -- that is, if "e" is the input and you have to find "E" then you can make 2 string arrays, or you could use the String method equalsIgnoreCase .
Once you find the index in the array of the input letter, you know the strudcture of what you will be producing. Use the same alphabet string array to print out the pyramid lines.
ASKER
I'm not a C programmer, but I can tell you what you need to do.
Firstly you are always printing 26 lines, but you only need x where x is the position of the letter input in the alphabet (well call this Z). This can be done by getting the ascii code of the letter. In VB there is an ASC function which does this ASC(Z). You then subtract the ASCII code for "A" and add 1. Noting that the input character is either upper or lower case, so convert it to upper case before calculating the number of rows needed. well call this number z
The next step is to work out the character to print. this will start at A, increment to the selected letter and then decrease back to A again. This can be done by using a variable lets call it x, if it is <= z then print char(x) else print char(2 * z - x)
I hope that this helps
Thank you ChloesDad. I like your elegant solution! But I believe mrcoffee365 is correct in saying that the instructor is looking for a loop based on a string array. We've just completed the Chapter on loops and are now in the Array Chapter of the text. I thank you for your valuable input and will keep your suggestion in my bag of tricks for future use.
ASKER
I thiink it would be easier to make a string array of the alphabet, and get the index in it of the letter you want. You could do ASCII decimal number calculations, but I bet your teacher is looking more for how to loop based on a string array.
So something like
String[] alphlist = {"A","B","C"}; // you get the idea
Then loop through alphlist until you hit the letter that equals the input. If you have to handle lower case vs upper case -- that is, if "e" is the input and you have to find "E" then you can make 2 string arrays, or you could use the String method equalsIgnoreCase .
Once you find the index in the array of the input letter, you know the strudcture of what you will be producing. Use the same alphabet string array to print out the pyramid lines.
Thank you mrcoffee365 for your suggestion, I believe that a loop based on a string array is what the instructor wants.
However, I am stuck again.
I've created the array,
String[] alphaList = {"A", "B", "C", "D", (all the way to 'Z'), "Z"};
so now I have to search the array for the char that the user entered (variable userInput).
What I tried was, after converting the userInput to a capital letter, I then search the array for the userInput.
userInput = Character.toUpperCase(userInput);// To capitalize letter
if (userInput == alphaList[0])
It believe I need to construct an 'if' statement that looks in the array for the userInput variable.
However, my 'if' statement (above) gives me an error "incompatible operand types char and string" (I'm using the Eclipse IDE). What it looks like is that the array is of 'string' type and the 'if' statement is trying to look for the 'char' type in the array and naturally it won't be able to do the search.
So now it seems like I need to convert the array into the 'char' type so the 'if' statement will be able to do the search. I've done some searching on the internet and there appears to be a way to do this but in looking at my text, the subject of converting a string array to a char array hasn't been covered so I think I might be going off the rails at this point.
Thoughts?
String input = null;
char inChar;
char[] ch = {'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'};
Scanner s = new Scanner(System.in);
do{
System.out.print("Input a letter :");
input = s.next();
inChar = input.charAt(0);
}while((input.length()>1)&&!(input.equals("exit")));
if(!input.equals("exit")){
for(int d =0;d<=(int)inChar-97;d++){ // obtain (print) all chars up to and including the one entered.
System.out.print(ch[d]);
}
}
Lines 17-19 give the set of chars you need for the pyramid. (The System.out.print() above is just to demonstrate the successful capture).
ASKER
mrcoffee365,
Please disregard my earlier post, I've since changed the array to the 'char' type.
I've created a 'char' array,
so now I have to search the array for the char that the user entered (variable userInput).
What I tried was, after converting the userInput to a capital letter, I then search the array for the userInput.
So far, so good(Eclipse didn't give me an error on the above 'if'). However, I'm having an issue writing the code for what I need to do next.
What I'm conceptualizing is that, if the userInput is == to the array reference [0], then execute the pyramid.
If it is not == to the array reference [0], then increment the array reference and then somehow get it to loop back up and check the userInput variable to the array reference [1]
So using my 'if' statement above, I also need an 'else' to increment the array reference
The above doesn't work, Eclipse is telling me "Syntax error, insert AssignmentOperator Expression to complete Expression" and also "The left-hand side of an assignment must be a variable".
I'm thoroughly confused now. So I need another variable?
Thoughts?
Please disregard my earlier post, I've since changed the array to the 'char' type.
I've created a 'char' array,
char[] alphaList = {'A', 'B', 'C', 'D', (...all the way to 'Z'), 'Z'};
so now I have to search the array for the char that the user entered (variable userInput).
What I tried was, after converting the userInput to a capital letter, I then search the array for the userInput.
userInput = Character.toUpperCase(userInput);// To capitalize letter
if (userInput == alphaList[0])
So far, so good(Eclipse didn't give me an error on the above 'if'). However, I'm having an issue writing the code for what I need to do next.
What I'm conceptualizing is that, if the userInput is == to the array reference [0], then execute the pyramid.
If it is not == to the array reference [0], then increment the array reference and then somehow get it to loop back up and check the userInput variable to the array reference [1]
So using my 'if' statement above, I also need an 'else' to increment the array reference
else
{
(arr[i]++);
}
The above doesn't work, Eclipse is telling me "Syntax error, insert AssignmentOperator Expression to complete Expression" and also "The left-hand side of an assignment must be a variable".
I'm thoroughly confused now. So I need another variable?
Thoughts?
ASKER
Thank you krakatoa,
I copied your code and ran it in Eclipse and it gave the output like you said. I guess I'm in way over my head because I can't think of how to write the code to, first search the array to find the char that the user entered, and then how to display it in a pyramid. I'm sure there's a simple way to do it (because I'm in an introductory programming course) but I just can't seem to grasp how to write the code to do what I want.
I pseudocode:
1. have user input a single letter
2. search the array to find the letter
3. print all of the letters up to and including the user input letter in a pyramid.
It sounds really simple but I can't grasp the syntax of the coding.
I bought "Java for Dummies" and it has been helpful but I can't seem to apply the concepts to the problem I'm supposed to solve. I can't go from concept to execution. I need a "Java for Really Dumb Dummies" book.
I copied your code and ran it in Eclipse and it gave the output like you said. I guess I'm in way over my head because I can't think of how to write the code to, first search the array to find the char that the user entered, and then how to display it in a pyramid. I'm sure there's a simple way to do it (because I'm in an introductory programming course) but I just can't seem to grasp how to write the code to do what I want.
I pseudocode:
1. have user input a single letter
2. search the array to find the letter
3. print all of the letters up to and including the user input letter in a pyramid.
It sounds really simple but I can't grasp the syntax of the coding.
I bought "Java for Dummies" and it has been helpful but I can't seem to apply the concepts to the problem I'm supposed to solve. I can't go from concept to execution. I need a "Java for Really Dumb Dummies" book.
I can't think of how to write the code to, first search the array to find the char that the user entered,
I thought I'd done that bit for you. The character the user enters is in the variable 'inChar' in the code. Because char and int are convertible one to the other, you only need to subtract 97 (the int value of the letter 'a') from inChar's indexes to find the position in that character in the array.
-----------
Alternatively, you could declare a String "ABCDE . . . Z" and do a simple for() loop which prints out substrings of that String using maths on the ASCII codes.
String input = null;
char inChar;
String alphabet = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Scanner s = new Scanner(System.in);
do{
System.out.print("Input a letter :");
input = s.next();
inChar = input.charAt(0);
}while((input.length()>1)&&!(input.equalsIgnoreCase("exit")));
if(!input.equals("exit")){
int pointer = 1;
for(int y=0;y<=alphabet.indexOf(Character.toUpperCase(inChar));y++){
System.out.println(alphabet.substring(0,pointer++));
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
krakatoa: You've actually done this person's homework for them. Would you edit your answer and just give advice and possibly small code examples?
Sorry - my understanding was that if an OP posted code, that it would be ok to post corrected code, which is what mine is . . . ? If that's wrong, then I've gravely misunderstood the params.
krakatoa - no, I think you're right -- but posting the entire program looks an awful lot like doing the homework.
Right, no I wouldn't have done that. I think what the OP posted was as near as can be.
ASKER
Like I said in my original post, I'm not looking for someone to do my homework.
Here's my finished program, sorry I wasn't able to post it sooner.
Thank you for the help.
Here's my finished program, sorry I wasn't able to post it sooner.
Thank you for the help.
import java.util.*;// For using Scanner class for input
public class LetterPyramid
{
public static void main(String[] args)// Main method
{
char userInput = ' ';// Declare and initialize variables
char line = ' ', space = 0, letter = 0;
Scanner cin = new Scanner(System.in);
System.out.print("Enter a single letter: ");// Prompt for single character
userInput = cin.next().charAt(0);// Get single character
if (Character.isLetter(userInput))// Check to see if input is a letter
{
userInput = Character.toUpperCase(userInput);// To capitalize letter
for(line = 'A'; line <= userInput; line++)
{
for(space = userInput; space > line; space--)
{
System.out.print(' ');// Displays one space
}
for(letter = 'A'; letter < line; letter++)
{
System.out.print(letter);
}
for(letter = line; letter >= 'A'; letter--)
{
System.out.print(letter);
}
System.out.println();
}
}
else
{
System.out.println("Error:Invalid letter " + userInput);// If character is not valid letter display error
}
}
}
Thanks but it would be better to get this question answer changed to the one you provided yourself, as well as get your points back, as it's actually far better than my solution! Guess I didn't think hard enough about the spacing issues.
Thanks k.
Thanks k.
The only other thing I would add is that the assignment parameters were bad, inasmuch as your C and D assignment terms are not followed if you produce output that is essentially palindromic.
"Up to and including" - to me - means, if your input letter is "D", then the series is "ABCD". But your programme pivots on the input character, and so produces output of "ABCDCBA". This is not what the narrative asks for.
Your code forms a symmetric pyramid because there is always an odd number of characters to each line. This is not the case in my code, because it attempts to adhere to the proper interpretation of the task.
"Up to and including" - to me - means, if your input letter is "D", then the series is "ABCD". But your programme pivots on the input character, and so produces output of "ABCDCBA". This is not what the narrative asks for.
Your code forms a symmetric pyramid because there is always an odd number of characters to each line. This is not the case in my code, because it attempts to adhere to the proper interpretation of the task.
ASKER
That's a good point. The instructor didn't word the problem exactly as you described, which would have been more accurate however, I did include the example output in my original post that the instructor wanted. It's all good, your assistance was definitely helpful, thanks again.
Firstly you are always printing 26 lines, but you only need x where x is the position of the letter input in the alphabet (well call this Z). This can be done by getting the ascii code of the letter. In VB there is an ASC function which does this ASC(Z). You then subtract the ASCII code for "A" and add 1. Noting that the input character is either upper or lower case, so convert it to upper case before calculating the number of rows needed. well call this number z
The next step is to work out the character to print. this will start at A, increment to the selected letter and then decrease back to A again. This can be done by using a variable lets call it x, if it is <= z then print char(x) else print char(2 * z - x)
I hope that this helps