magmag magmag
magmag magmag

Reputation: 119

make a word centered in a line in java

guys i am trying to print an input word in a input line in equilateral traingle and this word should be centered ... here is my code

public void draw (){

    int starTosStart = 0;

    for (int rows=1; rows <= getHeight(); rows++)
    {
        String charToPrint = "*";
        String wordToPrint = "";
        if(rows == getRowNum()){
            starTosStart = ( getRowNum() - getTextLabel().length() )/2;
            for(int i=0;i<getTextLabel().length();i++){
            wordToPrint += " "+getTextLabel().charAt(i);
            }
        }

        for (int spaces=1; spaces <= number_of_stars; spaces++)
        {
            System.out.print(" ");
        }
        for (int star=1; star <= rows; star++)
        {
            if(rows == getRowNum()){

                System.out.print(wordToPrint);
                System.out.print(" ");
                break;
            }
            System.out.print(charToPrint);
            System.out.print(" ");
        }
        System.out.println("");
        number_of_stars = number_of_stars - 1;
    }
    }
}

my output is

      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
 Z E L D A 

while the output needed is

      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* Z E L D A * 

any help please ? thanks you

Upvotes: 0

Views: 72

Answers (2)

Thomas
Thomas

Reputation: 88737

You could treat the word as an array of characters and use the length of that array to "replace" the relevant stars in the last line with the appropriate character from that array.

So basically you'd calculate the offset (where to start replacing the stars) as offset = number_of_stars - word.length() / 2 and then use word.charAt(star - offset) to get the characters.

I'll add an example if I have time. Just one thought though: how would you handle words that won't fit that last line? How would you handle words of even length in a row that contains an odd number of stars?

Update: Example for using charAt() but without handling even word length vs. odd number of stars.

public void draw ( String word, int height ) {
  int wordOffset = (height - word.length())/2 ;        

  //loop through lines
  for (int row=0; row < height; row++) {
    int starOffset = height - row;

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

    for (int star = 0; star <= row; star++) {
      char charToPrint = '*';

      //determine if we're inside the word and if so replace the star with a word character
      if(row == height - 1 && star >= wordOffset ) {                    
        int charIndex = star - wordOffset;
        if( charIndex < word.length() ) {
          charToPrint = word.charAt( charIndex );
        }
      }

      System.out.print(charToPrint);

      //print space if not just printed the last "star" in that line
      if( star < row ) {
        System.out.print(" ");
      }    
    }
    System.out.println();
  }
}

Outputs for "Zelda" and heights 7 and 8:

                           *
       *                  * *
      * *                * * *
     * * *              * * * *
    * * * *            * * * * *
   * * * * *          * * * * * *
  * * * * * *        * * * * * * *
 * Z E L D A *      * Z E L D A * *

Upvotes: 0

Jordi Castilla
Jordi Castilla

Reputation: 26981

You already got it, simply change this lines:

 System.out.print(wordToPrint);
 System.out.print(" ");

By this

 System.out.print("* " + wordToPrint + " *");

Upvotes: 1

Related Questions