Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer problem

 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
is there any limitation to the size of StringBuffer...
I append few thing in StringBuffer, but when i output them, few lines are lost...
hope I am clear
tia
MB
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add your code
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the code

tia
malhar
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the specification,
"Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger",
hence it should not give any problem, but you can try printing its capacity (using sb.capacity())
at different places at the end , if it varies. well there mite be some mistake in ur code
 
Linda Jones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would there be any difference between calling out.println(sb)
and out.println(sb.toString()) ?
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Linda said send the StringBuffer as a String using sb.toString(); .
But also, don't forget to flush the stream:
with out.flush(); or out.close();
 
Andrea Gazzarini
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Linda, the two lines of code are perfectly the same thing.
regarding your problems I can suggest two things:
1)you can specify the size of the StringBuffer as an int variables so I suppose a StringBuffer can't contains (Integer.MAX_VALUE + 1) chars;
2) You insert in the StringBuffer an HTML page. I suppose you use this in a Servlet. In this case, look at the html code, because if there is a syntax error, the page is printed until the error is reached. This not means the StringBuffer doesn't hold your string, but simply the Browser (IE, NEtscape??) html parser doesn't recongnize the rest of the page.
Hope it helps.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
the first thing i would have done is,
S.o.p(sb.toString()); which should print content to the Server screen (where we started the server) and then copy & pasted to a HTML file and try viewing it in some of the browser.
regards,
maulin
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrea Gazzarini:
Hi Linda, the two lines of code are perfectly the same thing.
regarding your problems I can suggest two things:
1)you can specify the size of the StringBuffer as an int variables so I suppose a StringBuffer can't contains (Integer.MAX_VALUE + 1) chars;


2) You insert in the StringBuffer an HTML page. I suppose you use this in a Servlet. In this case, look at the html code, because if there is a syntax error, the page is printed until the error is reached. This not means the StringBuffer doesn't hold your string, but simply the Browser (IE, NEtscape??) html parser doesn't recongnize the rest of the page.
Hope it helps.

well andrea
i dont think there's any problem with HTML code as this is the code supplied to me by Front Page. It is just that i have to get the values printed from database between this HTML tags.
I had to ask abt the size problem just because, if i remove some line that i have put as comments in HTML tag, the same number of line are then appended to the output & as a result i can see more HTML stuff.
hope i m clear
MB
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic