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?

Recommended Answers

All 2 Replies

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

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.