• Post Reply Bookmark Topic Watch Topic
  • New Topic

string characters first three multiple times  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying below challenge to display first three characters three times if the size of the string is greater than 3.

Say if i send hello i should get helhelhel

if i send xyz i should get xyzxyzxyz

I wrote as below



I am getting error as below

Error: public String front3(String str) {
^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

Please advise. thanks in advance
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're on the right track. If neither of the conditions are true, then you don't return anything (void). The method must return a string of some type.

Also, your second condition doesn't triple the string. It would return "hel".
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be a string returned for every possible execution path. Ask yourself what your method returns if you pass it the string "ab".
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Why not use str.substring(0, 3)?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:I am trying below challenge to display first three characters three times if the size of the string is greater than 3.

if i send xyz i should get xyzxyzxyz

Not according to your specification, but even if you do need to there is no need to make a string of length 3 a special case.
Using either charAt or substring will work with strings of three characters.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!