1

I have been trying to figure out this problem, any help would be appreciated. I am supposed to create a header of asterisks which is supposed to be a method like this:

enter image description here

I am completely stumped, I am able to create the two long lines of 60 asteriks and the left most 3 asteriks but can't get the last 3 or have any clue how to get the message to print.

this is what I have to print just the basic lines:

for (int x = 0; x <= 60; x++){
    System.out.print("*");
}
System.out.println("\n***");     
for (int x = 0; x <= 60; x++){
    System.out.print("*");
}
System.out.println("");

which prints out:

enter image description here

Any help would be great.

Edited code:

public static void main(String[] args)
{

    String msg = "";
    Scanner in = new Scanner(System.in);
    int whiteSpace;


    System.out.println("Enter your message: ");
    msg = in.nextLine();

     if(msg.length() < 54)
    {

        for (int x = 0; x <= 60; x++)
        {
            System.out.print("*");
        }
        System.out.println("\n***");


         whiteSpace = (54 - (msg.length())/2);
         for (int x = 0; x<=whiteSpace; x++)
         {
         System.out.print(" ");
         }

         System.out.print(msg);

         for (int x = 0; x<=whiteSpace; x++)
         {
         System.out.print(" ");
         }

       System.out.print("\n***");

       for (int x = 0; x <= 60; x++)
       {
           System.out.print("*");
       }
       System.out.println("");

}

}

and I get this:

enter image description here

rKittle
  • 43
  • 6

2 Answers2

1

you're missing the entirety of your string and the tailing 3 *s.

you have 54 characters between the two sets of '***' so to center your string you need to take (54 - (string.length()))/2 and add this much white space at the beginning AND and of your string before finishing with the remaining 3 *s (you should also check that the string is a max of 54 characters.)

for (int x = 0; x <= 60; x++){
    System.out.print("*");
}
System.out.println("\n***");


//your code is missing this
int length=string.length()
int whiteSpace=(54 - (string.length()))/2)
for (int x = 0; x<=whiteSpace){
 System.out.print(" ");
}
System.out.print(string);
for (int x = 0; x<=whiteSpace){
 System.out.print(" ");
}


System.out.print("\n***");
//up to here


for (int x = 0; x <= 60; x++){
    System.out.print("*");
}
System.out.println("");
Jason V
  • 593
  • 1
  • 8
  • 27
  • I intentionally left this a bit pedantic so you could see the linear translation of the idea. – Jason V Oct 30 '17 at 20:44
  • Hi, thanks for the help! can you check my edited code in my post and tell me whats wrong? Thanks again. – rKittle Oct 30 '17 at 21:15
  • @rKittle fixed in the comment on your question. but ill post here also. you're missing a set of parentheses now. it needs to be (54-msg.length)/2 not 54-(msg.length/2) .... so move the parenthesis before msg.length to before the 54:) – Jason V Oct 31 '17 at 14:37
1

Follow this approach to complete the task:

  • Identify the line in your code that prints the middle three ***
  • You need to print message followed by another three *** and \n
  • The total number of characters on the middle line is 60, so the text in the middle has to be 54-characters long
  • Therefore, message needs to be truncated at 54 characters
  • Assume that the message is N characters long, where N <= 54
  • Padding on the left is padLeft = (54-N)/2
  • Padding on the right is padRight = 54 - N - padLeft
  • After that the message needs to be padded on both sides until it is exactly 54-characters long
  • Once you have your padded message, print "***" + paddedMessage + "***"

See this Q&A for information on how to pad a String in Java.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399