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 :: Java Static Top-level Class 
Java :: enhanced 4 loop 
Java :: edit activity main drawer items text color android 
Java :: Which type of Exception will be thrown by forName() method 
Java :: adding Executable Jars to Spring Maven Project 
Java :: TestNG Data Provider example 
Java :: what is the import for gogga class java 
Java :: java 8 list of objects get only first occurance 
Java :: how to open a desktop application using java 
Java :: Java Floating-point Literals 
Java :: concatenar java 
Java :: expandablelistview android 
Java :: save ArrayList into Room Database android studio 
Java :: kotlin dependency injection 
Java :: Program to read base and power and then calculate result of that expression using recursion in java 
Java :: java how to make a recrussive boolean function 
Java :: JAVA XML COURSE 
Java :: 1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java 
Java :: binomial heap implementation java 
Java :: arraylistof objects 
Java :: cannot write data document seems to have been closed already in java 
Java :: java ternärer operator 
Java :: spring security specific url for specific account 
Java :: Dhttps.protocols=TLSv1.2 
Java :: constructor overloading ambiguity resolved 
Java :: contoh object dalam oop java 
Java :: Java Type conversion from int to String 
Java :: date to timestamp java 
Java :: java android join array list 
Java :: extract html tag using regex 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =