Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question - Reversing user input

Options
  • 09-10-2013 3:26pm
    #1
    Registered Users Posts: 474 ✭✭


    Hi there,
    I am currently attending a Java course in Fás and I am having trouble with a particular question.

    This is the Question:
    "I need to reverse user input, without using StringBuilder".
    We haven't covered it yet and I want to wait for the Instructor to explain it to us.

    I would ask the instructor but a lot of times I am even more confused after!
    If anyone could help me it would be great.

    NOTE: I don't necessarily want the answer, what I would like is someone to explain it to me.

    Thank you very much.


Comments

  • Closed Accounts Posts: 8,016 ✭✭✭CreepingDeath


    You can use a for-loop to run through the characters from the end backwards to the beginning and add them to a new string.
    That will reverse the string from eg "ABCDE" to "EDCBA"


  • Registered Users Posts: 710 ✭✭✭mad turnip


    We had this question before in an interview and the girl literly submitted:

    new Stringbuilder(str).reverse()

    I couldn't stop laughing, there was another similar question of sorting a list and in that one she did:

    Collections.sort(list);

    I still can't stop laughing at it. And it was so cheeky she got the job!!!!!!!!!!!!


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    mad turnip wrote: »
    We had this question before in an interview and the girl literly submitted:

    new Stringbuilder(str).reverse()

    I couldn't stop laughing, there was another similar question of sorting a list and in that one she did:

    Collections.sort(list);

    I still can't stop laughing at it. And it was so cheeky she got the job!!!!!!!!!!!!

    Why wouldn't she say that, its a completely valid and correct answer. The **** up is on the interviewer not defining the question correctly.


  • Registered Users Posts: 710 ✭✭✭mad turnip


    ChRoMe wrote: »
    Why wouldn't she say that, its a completely valid and correct answer. The **** up is on the interviewer not defining the question correctly.

    I agree completly! One would kind of presume not to do that but still funny!


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    • Find length of user input string
    • Use this to find last character
    • Minus by one to find second last character
    • Repeat until you reach first character


  • Advertisement
  • Registered Users Posts: 1,019 ✭✭✭carlmango11


    You can use a for-loop to run through the characters from the end backwards to the beginning and add them to a new string.
    That will reverse the string from eg "ABCDE" to "EDCBA"

    Wouldn't this be inefficient because Strings are immutable?

    Internally the computer would make a whole new string for:

    "E"
    "ED"
    "EDC"
    "EDCD"
    "EDCDA"

    Maybe building a String from a char array of the reversed String would be better


  • Closed Accounts Posts: 8,016 ✭✭✭CreepingDeath


    Maybe building a String from a char array of the reversed String would be better

    Well I didn't want to give the complete solution, just pseudo-code.

    Although using StringBuffer would be better again to build the new string.
    They only stopped them from using StringBuilder because it has a reverse() method in there already.


  • Registered Users Posts: 474 ✭✭Umekichi


    Thanks guys, solved it :D


  • Registered Users Posts: 1,019 ✭✭✭carlmango11


    They only stopped them from using StringBuilder because it has a reverse() method in there already.

    Yeah, stupid though because in the real world StringBuilder would be the way to go. They should really have just said you can't use StringBuilder.reverse


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,086 Mod ✭✭✭✭Tar.Aldarion


    then they ask you to recursively do it, good times.


  • Advertisement
Advertisement