This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getText into an Array  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First time poster, and new to Java in general. I'm in the middle of writing my first program and I'm hitting a brick wall!

The program is a Name List Editor that I building to help my wife out at work, it will function to remove white space before, after and in the middle of names and setup different tab spacing configurations that she needs. The newly modified list will show up on the right side where she can view and then save the file.
It consists of (and/or I want to achieve):
(2) text areas side by side, where a name list is pasted into the left side.
split each word into its own entry into an array.
add those entries into an array list.
be able to print out the list in the configuration needed (ex. FirstName LastName, or FirstName [tab] LastName [tab] Title, etc..)

I read all of the information that I can but I seem to learn so much better by seeing how something works and fiddling with it. Right now I'm just trying to get the spaces removed and the file to print out correctly on the right side. I'll paste the section of the code that I'm working on and I'd really appreciate some advice and guidance, or even if I'm taking the correct approach.

I did try the following but it didn't seem like it would work for any scenario other than removing spaces

Thank you for any and all help ;)
Posts: 2767
Posts: 1843
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fundamentally I think you're on the right track
Couple of comments on your approach:

Right now I think what you would get is one list:  John Doe John Doe John Doe John Doe. 
How are you going to tell where one name ends and another begins?

I would suggest breaking down the functionality into methods
Something like:

Also as a suggestion, while re-inventing the wheel can be fun some people have done this already:

Posts: 57476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you have got the text correctly split into individual tokens/words, which it would appear you are doing properly, you can use a StringBuilder (better than StringBuffer) to append the individual words followed by a single space, or simlar.
myStringBuilder.append(firstWord).append(" ").append(secondWord).append(System.lineSeparator());
Don't use escape sequences for line ends unless somebody has told you they want a specific line end, because you become platform‑specific.
You can also use a StringJoiner object. Beware: those features underlined were added in Java7 and Java8, so you will need a recent version of Java®. StringJoiner usually uses the same separator for every pair of entries, so I don't think it is suitable for adding line ends,
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!