The following piece of code is supposed to add html spaces in place of leading spaces.

 * Finds all leading spaces on each line and replaces it with
 * an HTML space ( )
 * @param s string containing text to replaced with  
 * @return the new string
public final static String leadingSpaces(String s) {
    s = noNull(s);

    StringBuffer str = new StringBuffer();
    boolean justAfterLineBreak = true;

    for (int i = 0; i < s.length(); i++) {
        if (justAfterLineBreak) {
            if (s.charAt(i) == ' ') {
            } else if (s.charAt(i) == '\n') {
            } else {
                justAfterLineBreak = false;
        } else {
            if (s.charAt(i) == '\n') {
                justAfterLineBreak = true;


    return str.toString();

I tried using this code in my application for formatting String text. he outcome includes "&nbsp" in its body. Could you please let me know what I am missing. The code snippet is from:

Recommended Answers

All 6 Replies

What is wrong with using the String.format() method or PrintWriter.printf() method?



It's suppossed to have


in it's body.

What are you viewing the output with, however. You show what you are doing, but what is this a part of?

A servlet, A JSP, a standalone application? There is a lot of missing context here.

Heres what I intend to do:

I am working on a JSF Application. The jsp page presented to the user should show him a table like looking text data.

The contents to be shown to the user using the jsp page are preconstructed in a Java Class. To display the String(text) data to the user in a formatted way is the ultimate motive.

String.format() is part of jdk(1.5) correct me if I am wrong! My Company has 1.4. So String.format() cannot be used.

As you had earlier suggested, padding spaces can be done. I tried this but the alignment of text fails.

I searched over the internet and found an example to use Formatter. This has the problem of &nbsp.

If this issue gets solved, The application passes the Acceptance Testing from the user. Please advice.


PS: Please let me know if there is any further info required.

Could you please let me know how this can be achieved using PrintWriter. I am relatively new to Java. So I am less confident in the usage of some of these concepts. It is not that I am not trying and simply asking in posts.

Hi Masijade, Please have a look at the output below:


CX333/14Mar08 12Q7 13Q6
CX555/14Mar08 1Q7 13Q6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

You can notice that in the 2nd Row, 13Q6 alignment does not match to the 1st Row. Although, I have used padding to Col length = 15. They show misalignment. This result is copied form a resultant .jsp page. In my console of the application, java output has come out perfectly like this:

[3/18/08 10:43:03:451 CST] 0000003d SystemOut O result of the OP ==>

CX333/14Mar08 12Q7 13Q6
CX555/14Mar08 1Q7 13Q6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
So it is something that the .jsp behaves. Please advice.

btw, the alignment that I am referring to from the console output is perfect. It does not show up correctly in this page also. Actually alignment is fine when I print it to the console.

Well, why don't you use a table? A table with a border, padding, and spacing of 0 will give the appearance of text columns.

The other option, since the console print looks correct, is to surround the block with "pre" tags.

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.