Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Switching the first and last letter of a string?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to write a method to switch the first and last letter of a string. For example the string "java" would become "aavj" here is what I have so far



With that code I get the following output "ajavaj" when I input "java" so how can I fix this? I need to switch the first two letters and still have the middle of the string. What should I do?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:With that code I get the following output "ajavaj" when I input "java" so how can I fix this? I need to switch the first two letters and still have the middle of the string. What should I do?

Well, the problem is in line 14, and you clearly understand how to use substring(). So how do you think you might use it to correct the problem?

Winston
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output:Final Output: hodeRancC
You better practice with String and StringBuilder functions first.
Edit:
John Sing wrote:public class printTest
{
class name should always start with capital letter like PrintTest rather than printTest. Please read this link
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be a great idea to write a method that would do the switching.
It is not a good style to put everything into main method.

What would this method is the string has length 0 or 1?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:It would be a great idea to write a method that would do the switching. What would this method is the string has length 0 or 1?
Agreed thoroughly! will keep in mind validations and clean code like creating separate method rather than writing in main. Thank you
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think a StringBuilder would be easier to use for that sort of thing.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I still think a StringBuilder would be easier to use for that sort of thing.
do you mean here?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:do you mean here?

No, but you're still using StringBuilder in a very clunky way. The whole point about it is that it's mutable, so you can change any character anywhere in the "string".

Consider this:<private rant>
Aaargh! Why can't designers be consistent???!!! If setCharAt() returned the old character value - like pretty much every 'setter' in the Collections framework does - that method could have been implemented in two lines.
</private rant>

@Ganish: Tip: NEVER return void if your method can usefully return something else.

Winston
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:but you're still using StringBuilder in a very clunky way
realized only when I saw your code
Winston Gutkowski wrote:@Ganish: Tip: NEVER return void if your method can usefully return something else.
yes, will keep in mind.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . If setCharAt() returned the old character value . . .
Isn't the reason that the StringBuilder methods return this so you can daisy‑chain the calls:-
. . . sb.setCharAt(a, charb)..setCharAt(b, chara);
Surely you would return
sb.toString()
and have String as the return type?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be nice, if setCharAt() returned `this`. But setCharAt is a void function - it returns nothing. It would also be nice if StringBuilder had a replace() method that took a char instead of a String.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . But setCharAt is a void function - it returns nothing. . . .
Damn! Never noticed. You probably can't change the return type (which does sound like a mistake) but adding that extra replace method sounds a good idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!