• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reversing a string without stringbuilder?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi l I have to reverse a string that the user inputs via an input box and I can't use the stringbuilder method. I have no idea how to do this. I was able to do this using stringbuilder but I don't even know where to begin without it. Here is my code using stringbuilder


So how can I do this with no stringbuilder?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to Java Ranch!

One of the rules here is we won't do your homework for you. However, we can give hints: check out the String method toCharArray().
 
Liutauras Vilda
Marshal
Posts: 4659
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:Hi l I have to reverse a string
Strictly speaking that is not possible. In case you forgot, remember, that String class is immutable. So it means once you created String class object, you cannot modify it.

What you likely mean is, either to (1)print String in reverse order, or (2)create a new String, which is reverse of given. So, before you start anything, check requirements what you have been asked for.

And welcome once again to the Ranch
 
Liutauras Vilda
Marshal
Posts: 4659
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And couple other things:

1. Class suppose to start with an upper case. That is conventional, so please follow it. It is important.
2.
John Sing wrote:So how can I do this with no stringbuilder?
Java is key sensitive, so it is not stringbuilder, but StringBuilder.
Ignoring key sensitivity sometimes could cause you compile errors, sometimes performance issues if you ever manage to write Double and perform calculations instead of double.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:Hi l I have to reverse a string that the user inputs via an input box and I can't use the stringbuilder method. I have no idea how to do this. I was able to do this using stringbuilder but I don't even know where to begin without it.

So how can I do this with no stringbuilder?


Have you learned loops yet? You can get individual characters of the string via charAt(), or get all the characters as an array -- then you can loop through it to reverse it.

Henry
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You cannot reverse the String because it is immutable. You can create a new String from it, or you can print its individual characters in reverse order.
Don't write imports for StringBuilder; since it is in the java.lang package that is unnecessary. Have you been told to use option panes? That is a very old‑fashioned programming style.You must pass an argument to the command line. Not
java reverse
but
java reverse "John Sing is a brilliant programmer."
Try with and without the quotes and see what happens.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:I was able to do this using stringbuilder but I don't even know where to begin without it.

The first thing to do is StopCoding (←click).

Sit down and work out, step by step, what you need to do to reverse anything, not just a bunch of characters in a String.

If need be, get a pack of cards and deal out a dozen of them face up on a table, and work out how you would reverse them. And note down every single step.

Now try and turn that into a program.

And the great thing about doing it that way is that you'll then be able to write a program that can reverse anything you like. And that's the real art of programming.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!