• Post Reply Bookmark Topic Watch Topic
  • New Topic

java program by asking the user for two strings and using the duplicate function  RSS feed

 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i doing this right?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added [code=java] code [/code] tags to make the post display nicer.

Your code works. You might want to add a space between s and t on the last line so it displays nicer.

One small improvement - see if you can use more meaningful variable names. You use "s" and "t" in different places to mean different things. Variable name are encouraged to be longer. See if you can think of a word that describes them. Even if it is just "string1" and "string2".
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify lines 4 and 5:
return s + s;

You will hear people say you should use StringBuilder for catenating Strings and the + operator has performance problems. That is absolutely true, but there are optimisations, so you can use the + operator on Strings as many times as you like, as long as they are all in the same statement. So s + s is in fact the best way to join two Strings like that.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can simplify lines 4 and 5:
return s + s;

Which is subjective. There is an advantage to using the temporary variable if using a debugger. (I don't use it often, but it is a legitimate thing to do.)

I tend to write return s+s; style and change it only if needed.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can simplify lines 4 and 5:
return s + s;


One could carry that forward a bit:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!