This is my first post, and I haven't contributed to the community yet but, I would be very glad if any of you Java experts, could help me with the following issue which has been driving me crazy this week.
The thing is that i'm querying a field that returns a very long String and then displaying it into a jTextarea that is line-wrapped.
The text gets correctly displayed, I mean the line breaks are shown.
After that, I need to update that text into the database splitted in lines, there's where my problem is, the jTextArea.getText() metod returns the original string which has no "\n".
Making it impossible to split,
Here are some of the solutions that i have tried:
a) String f=jTextArea.getText().split("\r?\n|\r"); (with a very long list of combinations for the line separators)
b) the method getLineEndOffset() hasn't work for me cause I have said the String ain't detected separated by lines but just as a very long string.
c) stringTokenizer - failed.
d) System. getProperty("line.separator") has failed also.
Line wrapping in the JTextArea does not modify the string by adding line breaks, it simply displays the (unbroken) string across multiple lines. If you want to split the text into lines, and the text does not have line breaks in it to begin with, then you will need to add your own line breaks.
I haven't done this before but my strategy would be:
1: Define the number of characters per line
2: Offset from the start of the string by the number of characters per line.
3: Search backwards from that point for good places to split (space, tab, -, etc...)
4: Insert a line break
5: Memorize the position you are at (after the line break)
6: Repeat steps 1-5, starting at the position memorized in step 5, and stopping when you have fewer characters left then the number of characters per line.
Joined: Feb 20, 2013
Hi Steve! and thanks for taking the time to response me.
Ok I'm giving it a try to the algorithym that you proposed.
That is great that you got it to work, Congrats! A few items to consider for future development and improvement:
1- when posting here, please UseCodeTags (<- link). Makes the code a lot easier to read, don't you think (I added them to your post for you)
2- lines 17-20 are a bit redundant. There is no need to treat aub == 0 as a special case, since aub = aub + ub would provide the same results. So those 4 lines could be done in 1 line, which is simpler, and simpler is always better.
3- Consider getting the String from the JTextArea just once and storing it locally. Maybe add Sting sourceString = jTextArea4.getText(); at the start of the code, then use sourceString wherever you had been using jTextArea4.getText(). This is more expressive (as long as you name the String variable something useful), means fewer method calls, lower chance of a mistype causing an error (like typing jTextArea3.getText()). You can still mistype sourceString but at least the compiler will find the error for you (rather than accidentally getting text from the wrong jTextArea and weird results happening). Finally, it protects you from a certain amount of danger if the value in jTextArea4 is changed while you are doing your work.
4- Your code here (which I assume is a method) does two things at the same time; it breaks the String into pieces and displays those pieces. You should practice designing code so that methods like this does one thing only. Since you have two things, I would consider using two methods, one which breaks the String into pieces, and one which displays pieces of Strings.
5- If you decide to do #4, then you should investigate StringBuilder, like Campbell said, it would be smoother if your goal is to actually build a single String with line breaks (rather than break a String into pieces)
I hope you realize these are criticisms, they are meant as next-steps you can follow to improve both the code and you Java skills.
Joined: Feb 20, 2013
Thanks Steve i'll keep in mind all of your advices :-)