• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this feasible use of Strings?  RSS feed

 
Greenhorn
Posts: 29
Firefox Browser Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity (me being new and all), is this the best way to use Strings or is there a simpler way? The reason I ask is the fact it took me a few lines to do it, and if I can shorten the method, well, all the better for time management and understanding of how this all ties together.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can definitely make it shorter and more manageable. System.out is a PrintStream and PrintStream has a method called printf() which lets you use format strings to produce output.

For example, you could do:

And get Well, Hello John! as output (with a newline at the end).
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general rule, I try not to store the same piece of data twice. So storing firstName, lastName, is fine, but then why do you also need to store them concatenated? The problem comes if you then want to re-use the firstName variable - you have to remember to reset everything else that depended on it...blargh...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!