• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner().useDelimiter(", ");  RSS feed

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my programm the names of actors are classified beginning with the surname such as Hewitt, Jennifer Love.
For the purposes of output, I have devised a method to reverse the name. It works well using a comma as delimiter, but if an actor just has one name such as Icecube, then the method is useless. Does anyone with experience using the Scanner class know how to include whitespace as well as a comma in the delimiters?
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a Scanner for this task seems like overkill to me.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your code which when applied not only perfectly reverses the actor names, but also the film titles in my program, even if the title or actor name is composed of one word. This is a new method in the String class.

 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The replaceAll, replaceFirst and matches methods were added to String in the 1.4 release (so they've been around considerably longer than Scanner). They're just convenience wrappers for the methods of the same names in java.util.regex.Matcher. The price of that convenience is that they recompile the regex every time they're called, which can get really expensive. You can avoid that expense by creating the Pattern object ahead of time and storing it:Then the reverse method would look like this:
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Alan for taking the time to respond with the extra advice.

The reverse() method is used on both actor names and film titles. Considering there are over 475 film titles stored in my program, your advice is readily applicable to my situation.

Now the film titles can be displayed in my JTable as there are naturally written with the article preceding the film name.

Thank you once again for your advice.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!