• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Switching the first and last letter of a string?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic