[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Add Line breaks in HTML using Java

Posted on 2011-09-19
21
Medium Priority
?
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 4
21 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36561012

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
ID: 36561035
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
ID: 36561041
plus, I'm looking for java code for that
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 47

Expert Comment

by:for_yan
ID: 36561312
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
ID: 36561371
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
ID: 36561394
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
ID: 36561443

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
ID: 36561452
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
ID: 36561475
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
ID: 36561579
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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36561584
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
ID: 36561601
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
ID: 36561626
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
ID: 36561631
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
ID: 36561638
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
ID: 36561657
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
ID: 36561705
jen0910: Can you provide a sample?
0
 
LVL 8

Expert Comment

by:Jen0910
ID: 36561805
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
ID: 36562051
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 500 total points
ID: 36562095
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
ID: 36588779
Thanks to both for_yan and jen
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

656 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