aspose file tools*
The moose likes Java in General and the fly likes String buffer issue when dispalying a new line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String buffer issue when dispalying a new line " Watch "String buffer issue when dispalying a new line " New topic
Author

String buffer issue when dispalying a new line

Makhana Jiggu
Greenhorn

Joined: Nov 09, 2005
Posts: 11
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.


Jiggu
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Makhana Jiggu
Greenhorn

Joined: Nov 09, 2005
Posts: 11
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Feb 03, 2004
Posts: 456
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

Joined: Nov 09, 2005
Posts: 11
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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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.
 
subject: String buffer issue when dispalying a new line
 
Similar Threads
String Question
Basic Java Problem....HELP!!!!
StringBuffer
StringBuffer question
I thought it should be "Hello" instead of "Hello how are you"