Problem Reversing String

Page 1 of 1

12 Replies - 1373 Views - Last Post: 08 October 2012 - 05:15 PM Rate Topic: -----

#1 rbk3ai  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 64
  • Joined: 08-September 12

Problem Reversing String

Posted 08 October 2012 - 05:44 AM

Hey im having a problem reversing string still dont have all the concepts down concrete have order a book to help seal the deal with this material, but here is the problem.


/*Complete the ReverseString program below so that it reads in a string of characters, reverses the order of the characters, and prints out the reversed string.

In other words, if the input string is "123456", the output string would be "654321".
If the input string is "How now, brown cow", the output string would be "woc nworb ,won woH".
*/


import java.util.Scanner;

/**
   Reads a string, reverses the order the characters within the
   string, and prints out the result.
   Input: the value of s, a string
   Output: the string reversed
*/
public class ReverseString
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      String s = in.nextLine();

     StringBuffer s = new StringBuffer
    System.out.println("Before reversing the string :"+s);
    s = s.reverse();
    System.out.println("After Reverse string is :"+s);    
      



   }
}



Is This A Good Question/Topic? 1
  • +

Replies To: Problem Reversing String

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem Reversing String

Posted 08 October 2012 - 05:51 AM

Are you allowed to use the reverse method? Normally when your doing it for school work, you have to do it manually. Doing it the way you do it now only shows you know how to call reverse. Either way, with the reverse method, just create a simple method
public String reverse(String s) {
    return new StringBuffer(s).reverse().toString();
}


Now take the input from the user, and pass the string to this method. The problem with your code at the moment is your using the same variable name (s) for both your String and your StringBuffer.
Was This Post Helpful? 1
  • +
  • -

#3 rbk3ai  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 64
  • Joined: 08-September 12

Re: Problem Reversing String

Posted 08 October 2012 - 06:06 AM

I'm not too sure if we are allowed to... the teacher doesnt make anything CLEAR! I bought a book and i'm trying to teach myself. I gave up trying to learn from the teacher its like the teachers that go from algebra 1 to geometry than back to algebra 2 half ass education system if ya ask me...

This post has been edited by rbk3ai: 08 October 2012 - 06:10 AM

Was This Post Helpful? -1
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem Reversing String

Posted 08 October 2012 - 06:25 AM

That method is doing the same thing as your doing now, but in a tidy way. The reason you are having problems at the moment is because your variable names are the same. Sort your variable names out so you dont have two called s.
Was This Post Helpful? 1
  • +
  • -

#5 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: Problem Reversing String

Posted 08 October 2012 - 06:50 AM

You can use a loop to store them in an array starting from the last location of the string (size)


and then store that array into a string.

This post has been edited by lilVaratep: 08 October 2012 - 06:59 AM

Was This Post Helpful? 1
  • +
  • -

#6 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

Re: Problem Reversing String

Posted 08 October 2012 - 08:57 AM

To make it easier you can use the split method on the string to store each number in an array and then iterate over the array in reverse order.
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem Reversing String

Posted 08 October 2012 - 03:17 PM

You are wasting our time
Your code won't even compiled
      String s = in.nextLine();

     StringBuffer s = new StringBuffer


You miss the parameter to the StingBuffer constructor
Was This Post Helpful? 1
  • +
  • -

#8 rbk3ai  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 64
  • Joined: 08-September 12

Re: Problem Reversing String

Posted 08 October 2012 - 04:58 PM

View Postpbl, on 08 October 2012 - 03:17 PM, said:

You are wasting our time
Your code won't even compiled
      String s = in.nextLine();

     StringBuffer s = new StringBuffer


You miss the parameter to the StingBuffer constructor


sorry my code wont even "compiled" maybe next time I will do it right and make it to were it will compile correctly for you... also your time means nothing to me! so if you dont like my thread or whats in it or dont like the fact im a complete noob and the material is moving way fast than go elsewhere and respond on someone elses thread FFS!!!
Was This Post Helpful? -4
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Problem Reversing String

Posted 08 October 2012 - 05:06 PM

It is your freaking homework not ours. Maybe programming is not for you.
http://www.dreaminco...mework-request/
Was This Post Helpful? 0
  • +
  • -

#10 rbk3ai  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 64
  • Joined: 08-September 12

Re: Problem Reversing String

Posted 08 October 2012 - 05:06 PM

i already got it correct douche bag see ya go troll somewhere else... thanks lilvara i ended up using a for loop and most def shout out to BURAK! thanks bro

This post has been edited by rbk3ai: 08 October 2012 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem Reversing String

Posted 08 October 2012 - 05:08 PM

You seriously pm'ing me for help and you have the cheek to talk to people like that? Sort your attitude out and then you may get people helping you. In the meantime, good luck with your work.
Was This Post Helpful? 0
  • +
  • -

#12 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: Problem Reversing String

Posted 08 October 2012 - 05:10 PM

LoL
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Problem Reversing String

Posted 08 October 2012 - 05:15 PM

I'm closing this thread for obvious reasons. Personal attacks will not be tolerated.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1