I just cannot figure out how to get the result I'm looking for. Here's my code
import java.util.Scanner;
public class StringInABox
{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a phrase or word!");
String Phrase = scan.nextLine();
String CapPhrase = Phrase.toUpperCase();
int PhraseLength = CapPhrase.length();
String SidePhrase = CapPhrase.substring(1);
int SidePhraseL = SidePhrase.length()-1;
for (int Letter = 0; Letter<PhraseLength; Letter++)
System.out.print(CapPhrase.charAt(Letter)+" ");
System.out.println();
for (int Letter = 0; Letter<SidePhraseL; Letter++)
System.out.println(SidePhrase.charAt(Letter));
for (int Letters = SidePhraseL-1; Letters>=0; Letters--)
{ for (int Space=0; Space <= PhraseLength*2-3;Space++)
System.out.print(" ");
System.out.println(SidePhrase.charAt(Letters));}
for (int Letter = PhraseLength-1; Letter>=0; Letter--)
System.out.print(CapPhrase.charAt(Letter)+" ");
}
}
The result is supposed to be something like:
H E L P
E L
L E
P L E H
But I can only get:
H E L P
E
L
L
E
P L E H
I've run out of ideas. I'm a beginner and this shouldn't take advanced coding.
You're quite close but the problem here is the order of the for loops. To get the required output as follows:
E L
L E
You need to print both of these characters in the same loop i.e. when it's printing on the same line. So you need the IndexFromFront and IndexFromBack variables which store the value of E and L respectively.
After your initial loop to print the characters H E L P
for (int Letter = 0; Letter < PhraseLength; Letter++)
System.out.print(CapPhrase.charAt(Letter) + " ");
System.out.println();
You need to start from the first index i.e. 1 to access E and the value of IndexFromBack would be PhraseLength-1-(IndexFromFront). Now that you have the indices of the values that you'd have to print from the original string, you need to get the right spacing i.e. 2*(PhraseLength-1)-1 because you have a space after each character. So your second loop to print the lines
E L
L E
should be as follows:
for (int Letter = 1; Letter < PhraseLength-1; Letter++) {
int IndexFromFront = Letter;
int IndexFromBack = PhraseLength-1-Letter;
// Print character from the start of string at given Index
System.out.print(CapPhrase.charAt(IndexFromFront));
// Print required spaces
for (int Space = 0; Space < 2*(PhraseLength-1)-1; Space++) {
System.out.print(" ");
}
// End space print
// Print character from the end of string matching the required index
System.out.print(CapPhrase.charAt(IndexFromBack));
// Printing on this line is complete, so print a new line.
System.out.println();
}
and the final line i.e. reverse of given input string can be printed using the loop as follows:
for (int Letter = PhraseLength - 1; Letter >= 0; Letter--)
System.out.print(CapPhrase.charAt(Letter) + " ");
}
Therefore the final code that you'd have is as follows:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class StringInABox {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a phrase or word!");
String Phrase = scan.nextLine();
String CapPhrase = Phrase.toUpperCase();
int PhraseLength = CapPhrase.length();
for (int Letter = 0; Letter < PhraseLength; Letter++)
System.out.print(CapPhrase.charAt(Letter) + " ");
System.out.println();
for (int Letter = 1; Letter < PhraseLength - 1; Letter++) {
int IndexFromFront = Letter;
int IndexFromBack = PhraseLength - 1 - Letter;
System.out.print(CapPhrase.charAt(IndexFromFront));
// Print required spaces
for (int Space = 0; Space < 2 * (PhraseLength - 1) - 1; Space++) {
System.out.print(" ");
}
// End space print
System.out.print(CapPhrase.charAt(IndexFromBack));
System.out.println();
}
for (int Letter = PhraseLength - 1; Letter >= 0; Letter--)
System.out.print(CapPhrase.charAt(Letter) + " ");
}
}
Here's a sample output for HELPINGYOUOUT
Enter a phrase or word!
H E L P I N G Y O U O U T
E U
L O
P U
I O
N Y
G G
Y N
O I
U P
O L
U E
T U O U O Y G N I P L E H
Edit: More readable code
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class StringInABox {
public static void printSpaces(int phraseLength) {
for (int space = 0; space < 2 * (phraseLength - 1) - 1; space++) {
System.out.print(" ");
}
}
public static void printTopRow(String capPhrase, int phraseLength) {
for (int letter = 0; letter < phraseLength; letter++)
System.out.print(capPhrase.charAt(letter) + " ");
System.out.println();
}
public static void printIntermediateBoxRows(String capPhrase, int phraseLength) {
for (int letter = 1; letter < phraseLength - 1; letter++) {
int indexFromFront = letter;
int indexFromBack = phraseLength - 1 - letter;
System.out.print(capPhrase.charAt(indexFromFront));
// Print required spaces
printSpaces(phraseLength);
// End space print
System.out.print(capPhrase.charAt(indexFromBack));
System.out.println();
}
}
public static void printLastRow(String capPhrase, int phraseLength) {
for (int letter = phraseLength - 1; letter >= 0; letter--)
System.out.print(capPhrase.charAt(letter) + " ");
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a phrase or word!");
String phrase = scan.nextLine();
String capPhrase = phrase.toUpperCase();
int phraseLength = capPhrase.length();
// Print the box
printTopRow(capPhrase, phraseLength);
printIntermediateBoxRows(capPhrase, phraseLength);
printLastRow(capPhrase, phraseLength);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With