This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getText into an Array  RSS feed

 
Chip Newman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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 ;)
-Chip
 
Norm Radder
Ranch Foreman
Posts: 2240
28
 
Stefan Evans
Bartender
Posts: 1836
10
  • 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:   https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#normalizeSpace(java.lang.String)

 
Campbell Ritchie
Marshal
Posts: 55748
163
  • 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,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!