Solved

Add Line breaks in HTML using Java

Posted on 2011-09-19
21
398 Views
Last Modified: 2013-11-23
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
Comment
Question by:Leo01
  • 9
  • 8
  • 4
21 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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
 

Author Comment

by:Leo01
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
plus, I'm looking for java code for that
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
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
 
LVL 8

Expert Comment

by:Jen0910
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
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
 
LVL 8

Expert Comment

by:Jen0910
Comment Utility
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
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Well, first try - if this splitting gives you acceptabe view - then it is fine, if not we'll think more
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
jen0910: Can you provide a sample?
0
 
LVL 8

Expert Comment

by:Jen0910
Comment Utility
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
 

Author Comment

by:Leo01
Comment Utility
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
 
LVL 8

Assisted Solution

by:Jen0910
Jen0910 earned 125 total points
Comment Utility
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
 

Author Closing Comment

by:Leo01
Comment Utility
Thanks to both for_yan and jen
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell output to HTML 1 28
Java / Linux and Regular Expressions 11 41
Currency Conversion? 1 33
Menu Inconsistent 3 18
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now