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) == ' ') {
                str.append("&nbsp;");
            } else if (s.charAt(i) == '\n') {
                str.append(s.charAt(i));
            } else {
                str.append(s.charAt(i));
                justAfterLineBreak = false;
            }
        } else {
            if (s.charAt(i) == '\n') {
                justAfterLineBreak = true;
            }

            str.append(s.charAt(i));
        }
    }

    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:

Dani AI

Generated

Two things are happening here, . First, if you see the literal text "&nbsp;" in the browser, your JSF view is escaping the output. By default <h:outputText> escapes HTML, so entities are shown as text. If you really want to emit &nbsp;, render it with escape="false":

<h:outputText value="#{bean.textWithNbsp}" escape="false"/>

Only do this if textWithNbsp is fully trusted, because disabling escaping can expose XSS. Safer alternative: do not generate &nbsp; at all and let CSS/HTML preserve spacing for you.

Second, the column misalignment you see in the JSP but not in the console is due to proportional fonts and HTML collapsing spaces. Use a monospaced font and tell the browser to keep whitespace and newlines:

<h:outputText value="#{bean.rawText}" escape="true" styleClass="mono"/>
/* CSS */
.mono {
  font-family: monospace;
  white-space: pre-wrap;   /* preserves spaces and \n, but still wraps long lines */
}

With this approach you can keep your original string (no &nbsp; conversion needed), and the browser will display it with the same alignment you see on the console. If you do not want wrapping at all, switch to white-space: pre.

Finally, since your data are truly tabular, ’s suggestion is the most robust: render a table. Either build a simple HTML <table> (or a JSF <h:dataTable>) and put each field in its own <td>. You will get consistent alignment without fighting whitespace rules, and you can control spacing with CSS instead of hardcoding spaces.

Recommended Answers

All 6 Replies

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

Edit:

Nevermind.

It's suppossed to have

&nbsp;

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.

Ok.
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.

Thanks,
Ashok.

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:

FROM FLIGHT Wgt REMARKS

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 ==>
FLIGHT Wgt REMARKS

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, networking, learning, and sharing knowledge.