• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Buffer value display  RSS feed

 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello All

i am using String Buffer in one of my project. please understand my problem and rectify it as soon as possible.
Thanks in advance

Problem: if i add more than one string in string buffer class object then i want that
Input-

Delhi India Tourism and Mr Prakash Pandey
Tourism of indian society and management and Mr Rahul Gupta
IIT and Mr Sachin Singh
Rural Academy and Ms deepika sahni

Output should be-

Organization Contact Person
Delhi India Tourism Mr Prakash Pandey
Tourism of indian society and management Mr Rahul Gupta
IIT Mr Sachin Singh
Rural Academy Ms Deepika Sahni

above data come from the database so length of the string vary, how i can do this with string buffer class.


 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leave above output because space removed from there see below output:

contact person come in same alignment corresponding to largest length of organization.

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur, please understand this is not a "code exchange site", but site dedicated to help other people learn. So you shouldn't expect other people will solve your problems instead of you (even less "as soon as possible"), and at the same time you shouldn't just hand correct code to beginners, as happened at some other occasions. We all learn best when we find solution ourselves. Please see NotACodeMill and EaseUp for details.

Now, back to your question: you must do the alignment yourself, by placing the correct amount of spaces between the two columns. Even though the text length can vary, you need to know the column width beforehand. Several possibilities exist: either inspect all data you get from database first and determine the longest string to get the correct width, or use the maximum field with from a database (most text fields usually have a length limit, though sometimes it is not set to a sensible number), or choose a reasonable fixed column width and either truncate, or allow to overflow fields that are wider than that (you might also wrap them, but it would be a tough work).

you should also use StringBuilder instead of StringBuffer; check out our String FAQ.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohkk thanks and will keep this in my mind.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[String].split() the input lines using ' ' (space) as a delimeter, then append each member of the resultant array with a ' '.
You may have a redundant space at the end, but very fast and efficient.

WP
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@william can not understand what you want to say...can you elaborate this more...
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William's advice can be used to remove duplicate spaces from a String. William probably missed your second post and assumed that you need to get rid of repeated spaces.

Did you try some of the approaches I've suggested earlier?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i take one variable, set width limit to 75, after that i retrieve organization from the database, finds its length and put spaces of diff of 75 and length of organization.

e.g:



then my output comes like above but is this good idea?

if i am using this code then i will same code more than 10 times and it increase my time and space complexity, so can you tell me any other good idea??
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the only thing I'd suggest to change on that code would be to replace sb.append(" "); with sb.append(' '); - ie. append the space as a single character, not a String, since that code path in StringBuilder is shorter (by a very very very little bit).

What do you compare the time and space complexity to? To the solution that wouldn't align the columns? Well, with aligned columns you do need the spaces, so there is no way around this. And regarding time complexity, the 10 times figure is way too much overstated. Profile both versions of your code, my bet is that you won't be able to tell the difference, since most of the time is spent retrieving the data from the database.

Maybe if you told us what you need to do with the contents of the StringBuilder afterwards, we might be able to find a more elegant solution. However don't be too obsessed with performance. Try to improve the performance only if there is a clear performance problem.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i am using thread based application so i can not switch to StringBuilder class. i was using jasper report to print the data in pdf format but there was a problem, if we use data in two textfield in jasper report textfield1 receive data dynamically and its size increases vertically on the basis of data and textfield2 data is a static data but here textfield1 override the data of textfield2.


i ask this question earlier but no one reply for this problem that's by i am sending both textfileld data from one StringBuffer class object to jasper report.

see below url

coderanch
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I cannot help with Jasper reports. Did you try its documentation? Also, I have to say that the question you asked about Jasper reports earlier seems a little bit unclear to me. If you add more details to that question and maybe the code you have, someone might be able to help. See also TellTheDetails.

Do you really need to use a StringBuffer? This would be the case if all your threads wrote to the same StringBuffer instance simultaneously, which would probably still need external synchronization. Also, the records in your report would come out in more or less random order (depending on the exact timing of threads), which would probably make the report confusing. IF your individual threads actually do not share common instance of a StringBuffer, you can use StringBuilder instead. It is better explained in one of the threads linked to from the String FAQ page.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i was try documentation of jasper and final answer is we can not solve this problem with jasper, for this i will need itext report but problem is that i am new to iText and i want to solve this problem ASAP.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!