Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to center a print statement text?

static void printer(String str, int size) {
    int left = (size - str.length()) / 2;
    int right = size - left - str.length();
    String repeatedChar = "-";
    StringBuffer buff = new StringBuffer();
    for (int i = 0; i < left; i++) {
        buff.append(repeatedChar);
    }
    buff.append(str);
    for (int i = 0; i < right; i++) {
        buff.append(repeatedChar);
    }
    // to see the end (and debug) if using spaces as repeatedChar
    //buff.append("$");  
    System.out.println(buff.toString());
}

// testing:

printer("string", 30);

// output:
// ------------string------------
Comment

PREVIOUS NEXT
Code Example
::  
::  
::  
::  
::  
Java ::  
::  
:: public class BigInteger { public static void main(String args[]) { long p=2147483648; } } 
::  
::  
::  
Java ::  
::  
::  
Java ::  
::  
::  
::  
::  
::  
::  
::  
Java :: run java in cmd 
Java ::  
Java ::  
Java ::  
Java ::  
::  
::  
::  
ADD CONTENT
Topic
Content
Source link
Name
8+4 =