• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string reversal problem

 
Jules Strand
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Im quite new at java and im trying to very simply take in a string and spit it out the other way around in a function. Heres what i have so far:

All i get is no output, what am i doing wrong?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jules,
Welcome to JavaRanch!
First of all, let me point out that while we don't have a lot of rules here at the Ranch, we do have our naming policy that requires youto use a not-obviously-fictional display name. Most folks round these parts just use their real names -- it keeps us honest. Please head over here and change your, pronto!
Now, as to your program: looking pretty good, actually. A few things I'd do:
  • Take the creation of reversedTextString out of the loop -- just create it once at the end, after the loop.
  • reverse() ought to return the String, but not print it; leave the printing up to main().
  • Speaking of main() -- it ought to call reverse(), yes? Probably using args[0], the first command-line argument, as the parameter.

  •  
    chi Lin
    Ranch Hand
    Posts: 348
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    while it is definitely a positive experience
    to implement function by yourself, API could
    be a good resources for other options available.
    check reverse() in StringBuffer class.
    I usually follow this procedure to check/verify my implementation.
    regards
    [ November 12, 2003: Message edited by: chi Lin ]
     
    Jules Strand
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey again, tryed to do what you said but i admit i didnt quite understand everything you suggested.
    I tryed to change the code and now it looks like this:

    I now get this error saying that no entity called "reversedTextString" was found in the main enviroment. How do i tell it i just want to print out the value of reversedTextString in reverse() bit?
    All i want this to do is to spit out the text "Hello" backwards. Is there an easyer way of doing this with a string?
    Thanks for all your comments, il try and change my name now, didnt realise it would be my posting name.
    Jules
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's see. You're getting that error because you're trying to get main() to print out a variable that's not defined in main, but defined in reverse(). What you want to print out in main is the return value of calling reverse().
    For main to be calling reverse(), it would have to mention reverse(), yes? The same way that, for example, reverse() itself calls charAt(). i.e.,
    System.out.println(reverse(args[0]));
    I just noticed that reverse() is returning its argument, rather than returning reversedString, as it should.
    Again, you should be constructing the String outside the loop, afterwards, not inside.
     
    chi Lin
    Ranch Hand
    Posts: 348
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    some more changes,

    [ November 12, 2003: Message edited by: chi Lin ]
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    chi lin --
    If you describe in words how to go about doing something, then you're helping the person learn. If you on the other hand just do their work for them, then they don't learn anything. Please refrain from doing this kind of edit-job in the future.
     
    chi Lin
    Ranch Hand
    Posts: 348
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ernest,
    I don't mean to mess with people's code or doing their job, just
    trying to pin-point out the problem on Jules' code as Jules seems
    has trouble follow "words" decscription closely at this stage.
    (a common syndrome observed on people that are not very experienced).
    Will be more careful with similar situation.
    [ November 12, 2003: Message edited by: chi Lin ]
     
    Dan Walin
    Ranch Hand
    Posts: 109
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems that Jules comments say he moved the construction of the string outside of the loop but the code shows that he did not.

    {

    reversedText[i] = s.charAt(text.length() - i - 1); // change to s.charAt() reversedTextString = new String(reversedText); // move outside the loop
    }
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic