Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Reversing a string without stringbuilder?

 
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 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?
 
Sheriff
Posts: 3045
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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: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
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic