This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String buffer issue when dispalying a new line

 
Makhana Jiggu
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the following code, the output is not displaying in a new line but displays everything in a same line. Any advice please. I'm struggling but cant get it solved. this is kind of urgent any help is great...

StringBuffer s = new StringBuffer(4000);
s.append("Name: " + custNamerequestedAmt + "\n");
s.append("Name: " + custNamerequestedAmt+"\n");
//s.append('\n'); // also tried this but did not work
s.append("Phone: " + custPhone + "\n");
s.append("Email: " + custEmail + "\n");
s.toString();

Also I want to bold the headings of the column the name and phone, how can i acheive this.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Is this in a servlet or JSP, so the output is going to HTML? If so, then you have to use explicit "<BR>" elements as line breaks; "\n" will be ignored. Of course, it would be better to use a "<TABLE>" element to structure your whole table; while you're at it, you could use "<TH>" (table header) elements for the headers, suitably styled.
 
Makhana Jiggu
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not using servlet.
I'm using java and output is displayed in a jsp page using jsp custom logic tag.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, a JSP. So the output is HTML. As I said, newlines aren't significant in HTML; you have to use HTML tags for formatting. Your code is going to have to do something like this:



Of course, there are better ways to do this, but you get the idea.
 
Jan Groth
Ranch Hand
Posts: 456
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my two cents:

from java 5 on, use the StringBuilder instead of the StringBuffer. Its not synchronized, which is a completely unnecessary functionality in most use-cases.

many greetings,
jan
 
Makhana Jiggu
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use existing jsp page where i need to pre populate all these values in a text area in a new line. Is there any other solution?
 
Paul Clapham
Sheriff
Pie
Posts: 20187
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what you need to do:

1. Decide what HTML you need to produce.

2. Write a JSP to produce that HTML.

Sounds like you haven't done step 1 properly because you aren't clear on how newlines work in HTML. I thought you didn't have to change them to <br> in a text area but I'm not clear on that either. So just create some little HTML pages in a text editor and see what works.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic