• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading Text From a JtexArea

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends, I hope you're all fine!

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.

what other alternative do I have?

Thanks in advance for any of your comments guys.











 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carlos Valderrama
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve! and thanks for taking the time to response me.

Ok I'm giving it a try to the algorithym that you proposed.

I'll let you know !


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Remember that Strings are immutable. Put the String into a StringBuilder, which is specially designed for changes like that.
 
Carlos Valderrama
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! and thank you for welcoming me at the ranch!

I finally got it working, it cost me a lot of time but i made it following the recommendation given by Steve

here's the code

I hope it helps if anyone else needs it




Being everytime "cadena" gets printed the content of the lines of the jTextArea.

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carlos Valderrama
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve i'll keep in mind all of your advices :-)

 
We don't have time to be charming! Quick, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic