I am making a project in Number Theory and i want to display
some formatting text in a textArea in my Gui.
I have a method named padding which takes an integer as an argument
and return a String consisitng of a number of spaces.
This number is 12 - the number of the given integer length.

     public String padding(int number){
               int NumberOfDigits ;
               String padding = "";
                NumberOfDigits = 12 - Integer.toString(number).length();
                    for (int j = 0; j < NumberOfDigits; j++) {
                       padding +=" "; 
                        }
                    return padding;
           }

I am using this method like this :

            for (i = 2 ; i < number; i++){
              if (isPrime(i) && (number % i == 0)){
          textAreastring +=   padding(number) + number + padding(number) + " : " + padding(i) + i + padding( i) + " = " + padding( number / i) + number / i + " " + newline;
                    listint.add(i);
                    number = number/i;
                    i--;                        
                }

Is there a way using String methods to produce the same formatting?

Edited 3 Years Ago by nikolaos

Yes.

String's format method uses java.util.Formatter format specs (based on C's printf) to format data into strings with specified padding, decimal places etc etc etc

This question has already been answered. Start a new discussion instead.