[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

Add Line breaks in HTML using Java

I want to be able to add line breaks in HTML String buffer. In some cases the browser is not rendering correctly.
Say for every 100 chars, I would like to check if I can add a break and add if it is not in the middle of the sentence or space.
0
Leo01
Asked:
Leo01
  • 9
  • 8
  • 4
2 Solutions
 
for_yanCommented:

Just  make sure you put "<br>" among the HTML which you are printing from the servlet, probably like that:

out.println("<br>")


<br> will make endo fline in HTML
0
 
Leo01Author Commented:
for_yan,
I know that I have to add <br>. I do not want to add at random for every 100 chars.
I want to be able to add intelligently so that it does not look stupid on the page.
0
 
Leo01Author Commented:
plus, I'm looking for java code for that
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
Well, we sure can come up with some code which would split youtr text into words
and then check if number of charcatcters is becoming molre than 100 insert <br> ,
but I wrote many servlets and almost never ad to do such thing - that's because browsers do a pretty good job
in fornattimng the text.
Do you have some special kind of text?
0
 
Leo01Author Commented:
This is a tool that converts documents to html. In some special cases, it is failing. So I want to be able to add <br> in HTML String buffer that I have.
0
 
Jen0910Commented:
Can you control the width of the div element your text is in, or the width of the <p> tag your text is in? If you can I'd set the width to be close to the width of 100 characters through css. Should save you some time.
0
 
for_yanCommented:

I think something like that could work:

                            String sep = System.getProperty("line.separator");
        String myText = "abc derf hjkl mnbg " + sep    +
                " ghjj sadasd  asdasd";

       String [] array = myText.split("\\s+");
        
        for(String ss1: array) {
            System.out.println(ss1);
        }

        StringBuffer sb = new StringBuffer();
        int len = 0;
        
        for(int j=0; j<array.length; j++){
            
            sb.append(array[j]);
            len+= array[j].length();
            if(len > 90)sb.append("<br>");
            
        }
       
        System.out.println(sb.toString());

Open in new window

0
 
for_yanCommented:
corrected obvious mistake

                            String sep = System.getProperty("line.separator");
        String myText = "abc derf hjkl mnbg " + sep    +
                " ghjj sadasd  asdasd";

       String [] array = myText.split("\\s+");
        
        for(String ss1: array) {
            System.out.println(ss1);
        }

        StringBuffer sb = new StringBuffer();
        int len = 0;

        for(int j=0; j<array.length; j++){

            sb.append(array[j]);
            len+= array[j].length();
            if(len > 90)sb.append("<br>");
            len = 0;

        }

        System.out.println(sb.toString());

Open in new window

0
 
for_yanCommented:
this is better, I hope

                            String sep = System.getProperty("line.separator");
        String myText = "abc derf hjkl mnbg " + sep    +
                " ghjj sadasd  asdasd";

       String [] array = myText.split("\\s+");
        
        for(String ss1: array) {
            System.out.println(ss1);
        }

        StringBuffer sb = new StringBuffer();
        int len = 0;

        for(int j=0; j<array.length; j++){

            if(len+ array[j].length() > 100){
                    sb.append(array[j] + "<br>");
                len = 0;
            }
            else  {sb.append(array[j] + "<br>");
                    len += array[j].length();
            }



        }

        System.out.println(sb.toString());

Open in new window

0
 
Leo01Author Commented:
for_yan, Thanks. I'll try and let you know.

Jen0910: can you be more specific ( with example), how fixing the width would help solve the problem?

If I copy the buffer to html file and load that in browser outside of the coed, the browser handles it correctly, with line-breaks and everything. I'm thinking it's more to do with Widths in css...so the question to Jen0910
0
 
for_yanCommented:
ell, yiou never xcan find mistakles until you really test it.
I hope this is working:

 
                            String sep = System.getProperty("line.separator");
        String myText = "abc derf hjkl mnbg " + sep    +
                " ghjj sadasd  asdasd  fgdf gdfgdf  dfgdfgd   fgdfgdf  dfgdf g  werwe tre te  rtyrty rtyrtyrt vvxcvx fgdgdfg vxcvxcv rtertert xcvxcvx ertertert xcvcx  eerwer c dsfsdfwerwe  " +
                "dsadas fsdfsd dsfsdfsd dsfsdfsd sdfaweq  qweq  fsdft  gdfg  ertert  s dfsdfs erttert dsdfsdf  "  + sep + "asdas sdfsdf dffgdf  fghfg  asdas   hfghf hjghjgh hjghjgh hfghfghfgh ghfg" +
                "fghfgh fghfg fghfgh fhghfg"; 

       String [] array = myText.split("\\s+");
        
      //  for(String ss1: array) {
        //    System.out.println(ss1);
       // }

        StringBuffer sb = new StringBuffer();
        int len = 0;

        for(int j=0; j<array.length; j++){

            if((len+ array[j].length()) > 100){
                    sb.append(array[j] + " <br>");
                len = 0;
            }
            else  {sb.append(array[j] + " ");
                    len += array[j].length() + 1;
            }



        }

        System.out.println(sb.toString());

Open in new window


abc derf hjkl mnbg ghjj sadasd asdasd fgdf gdfgdf dfgdfgd fgdfgdf dfgdf g werwe tre te rtyrty rtyrtyrt <br>vvxcvx fgdgdfg vxcvxcv rtertert xcvxcvx ertertert xcvcx eerwer c dsfsdfwerwe dsadas fsdfsd dsfsdfsd dsfsdfsd <br>sdfaweq qweq fsdft gdfg ertert s dfsdfs erttert dsdfsdf asdas sdfsdf dffgdf fghfg asdas hfghf hjghjgh <br>hjghjgh hfghfghfgh ghfgfghfgh fghfg fghfgh fhghfg 

Open in new window

0
 
for_yanCommented:
Test the last code - previous was just compiles, but the last ione I executed.

And again - think twice before you do that - thsi is the profession of the browseer - it will do it better than we can
0
 
Leo01Author Commented:
for_yan, I absolutely agree with you. I can send you the sample file privately, if you want to let me know the problem with html ( sorry can't post on the forum)
0
 
Jen0910Commented:
If you have code you can share with me, then I can add it directly to the elements you are using. Otherwise I can give you a general example. Which would you like?
0
 
for_yanCommented:
Well, first try - if this splitting gives you acceptabe view - then it is fine, if not we'll think more
0
 
for_yanCommented:
And also if you make up some model of your text - maybe it will help - because I'm coming form java
side - if your text is in some way special - the wy to do it - uyou'll get more help from HTML virtuosi
0
 
Leo01Author Commented:
jen0910: Can you provide a sample?
0
 
Jen0910Commented:
This examples uses a font at 12 px. A 100 character limit per line gives you a max width of about 710px. Keep in mind since you are going with a character limit, this width will change with a different font size.

To control the overall width of the text container to appx. 100 characters:
<head>
<style type="text/css">
#myText {font-family:Arial, Helvetica, sans-serif; font-size:12px; width:710px;}
</style>
</head>

<body>
This text is controlled to an aapx. 100 character limit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</div>
</body>

Open in new window


To control only the paragraph element where the text is displayed to appx. 100 characters:
<head>
<style type="text/css">
#myText {font-family:Arial, Helvetica, sans-serif; font-size:12px; width:100%;}

#myText p {width:710px;}
</style>
</head>

<body>
<div id="myText">some text here that is not controlled by your appx. 100 character limit of 710px set above. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

<p>this text is controlled to an aapx. 100 character limit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p></div>
</body>

Open in new window


Again, if you have code you can share I can apply this theory directly to your elements as I'm thinking you may have much more going on in your page than 1 box element ;)
0
 
Leo01Author Commented:
Only Style I have before this HTML is:
 <div style="margin:10px;">
<%=myhtmlDoc.replaceAll("position:absolute;", "")%>

can I change it to:
 <div style="margin:10px;width:100%;">
<%=myhtmlDoc.replaceAll("position:absolute;", "")%>
0
 
Jen0910Commented:
If you are using 12px font, try:

<div style="margin:10px; font-size:12px;">
<%=myhtmlDoc.replaceAll("position:absolute; width:710px;", "")%>

Open in new window


if that doesnt work, try:
<div style="margin:10px; font-size:12px; width:710px;">
<%=myhtmlDoc.replaceAll("position:absolute;", "")%>

Open in new window

0
 
Leo01Author Commented:
Thanks to both for_yan and jen
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 9
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now