user3571012
user3571012

Reputation: 91

How to make a loop to center a string?

How could I create a loop to make a space so i can output more spaces rather than less. The inputted string should be centered into the asterisks. I can not figure out how to get the for loops to do this. So if you could help me find out why or fix my code that would be great.

/*

*******
*     *
*  I  *
*     *
******* 
instead of 
***
* *
*I*
* *

*/

    public static void main(String[] args) {
    // TODO code application logic here
    Scanner keyboard = new Scanner (System.in);
    System.out.print("Enter a string: ");
      String word = keyboard.nextLine();
    int len=word.length();
    for(int i=0;i<len+2;i++)
    {
        System.out.print("*");
    }       
    System.out.println();
    System.out.print("*");
    for(int i=0;i<len;i++) //adjust the number of tabs
    {
        System.out.print(" ");
    }
    System.out.print("*");
    System.out.println();
    System.out.println("*"+word+"*");
    System.out.print("*");
    for(int i=0;i<len;i++) // adjust the number of tabs
    {
        System.out.print(" ");
    }
    System.out.print("*");
    System.out.println();

    for(int i=0;i<len+2;i++)
    {
        System.out.print("*");
    }
}

Upvotes: 1

Views: 433

Answers (3)

mrres1
mrres1

Reputation: 1155

import java.util.Scanner;
import static java.lang.System.out;

public class SomeClass
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        out.print("Enter a string: ");

        String word = input.nextLine();

        input.close();

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

        // First find out how many wide the first border should be

        int top = word.length() + 4/*spaces */ + 2/*asterisks */ ;

        // Print out that many asterisks
        while(top != 0) out.printf("*", top--);

        // Print out the second line, which is the length of top, except only 
        //   the first and last character are asterisks.
        out.printf("\n*  %" + word.length() + "s  *\n", " ");

        // Print out the word
        out.printf("*  %s  *", word);

        out.printf("\n*  %" + word.length() + "s  *\n", " ");

        int bot = word.length() + 6;

        while(bot != 0) out.printf("*", bot--);
    }

}

The console output:

    Enter a string: STACK OVERFLOOOWWWW


    *************************
    *                       *
    *  STACK OVERFLOOOWWWW  *
    *                       *
    *************************

Upvotes: 1

Biswajit_86
Biswajit_86

Reputation: 3739

I am not sure if you want only horizontal spacing or both horizontal and vertical spacing

If you want just horizontal spacing, then you can use the below

  1. calculate length of string (or max length of any row if it is multiple rows)-maxlen
  2. Decide how much spacing you want (lets call it x)
  3. Output asterisks of length 2+maxlen+(2*x)
  4. Output 2 asterisks spaced by maxlen+(2*x)
  5. Output asterisk, space,string,sapce,asterisk
  6. Repeat step 4
  7. repeat step 3

You can extend this to get vertical spacing as well

Upvotes: 1

Levenal
Levenal

Reputation: 3806

I have managed to wrangle a solution using a static method and some looping. Hopefully it can help with what you need. It should look correct regardless of the String length.

public class SO5 {
public static void main(String[] args) {
String st = "Photograph"; //String
    int length = st.length() + 4; //Length of string plus 4, for star and space at front and back 

    for(int i = 0; i < 9; i++){//Number of levels needed
        if(i == 0 || i == 8){//Start or end
            printForLength("*", length); //Print relevant number of start
            System.out.println(); //Next line
        } else if(i % 2 == 0){ //If an even number
            if(i == 4){ //If the middle
                System.out.println("* " + st + " *"); //Print star our string plus star
            } else { //If not middle
                System.out.print("*"); //Star
                printForLength(" ", length - 2); //spaces for length of string -2 for start and end stars
                System.out.print("*");//End star
                System.out.println();//New line
            }
        }
    }
}


//Method prints a character for a set number of times
public static void printForLength(String s, int length){ 
    for(int j = 0; j < length; j++){
        System.out.print(s);
    }
}

}

Good Luck!

Upvotes: 0

Related Questions