• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reverse string using stringbuilder  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to reverse a textfile using stringbuilder but can't get it to work, the text gets reversed but the rows end up in the wrong order.
This is the code:


If the file looks like:

1 abcd
2 ABCD
3 efgh

then I want it to be reversed to:

dcba 1
DCAB 2
hgfe 3

But this is the way it looks
.... 3
.... 2
.... 1
 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch.
try to visualize what's going in line 5.you are reading a line from file and then appending it to stringbuilder and then reversing,you come again to line 2 via loop and append it to the same stringbuilder and reversing again the entire stringbuilder which in result produces erroneous outcome as the line 1 which is reversed in last iteration get reversed again.
i think this much hint is enough to get your solution by yourself now.

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can easily visualize the bug in your program by adding some debugging like printing stringbuilder each time on standard output while iterating in a loop.

Kind Regards,
Praveen.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Despite what it says in many books, only use \n if somebody tells you they want the LF character. Use this instead:-
sb.append(scan.nextLine()).reverse().append(System.System.lineSeparator());
Note you can do all that in one line because most StringBulder methods end
return this; }

[edit]Note: that System class method only works in Java7/8/9
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
sb.append(scan.nextLine()).reverse().append(System.System.lineSeparator());

There's only one "System" though, right?  Just System.lineSeparator().
 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the answers.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . only one "System" though, right?  Just System.lineSeparator().
hahahahahahahahahahahahahahahahaha!

No, it's System.System.System.System.lineSeparator() And sorry for my mistake.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cliff Karlsson wrote:Thanks for all the answers.
Our pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!