• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Issue with reversing String leveraging String Builder

 
Ranch Hand
Posts: 186
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code, I have 2 methods. In my first method, I completely rely on StringBuilder and it's reverse() method. This seems to work. However, in my second method, I don't get anything returned. Why is this?


 
Rancher
Posts: 137
7
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if on line 19 you make the following change:

In the initial code, you start your iteration at the end of the sb2 object, which is empty because you just created it. You should start iterating from the end of the String object (input2).
 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:....

I guess you meant i--  rather than i++ at for update.

Why do you think you need to String variables? You don't need two String variables, only one is enough.

I think you don't need to declare String variables as static unless there is a valid reason it be static, either declare it as an instance ( non-static ) variable may be with private modifier? then you can initialize it using constructor or you can declare and initialise String variable as local variable in main method.
 
Brecht Geeraerts
Rancher
Posts: 137
7
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Ganesh, indeed... the force of habit I presume. Thanks for spotting it. #slightlyembarrassed
 
Ganesh Patekar
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ BG that's fine, it happens with everyone  
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!