2

Cant seem to get this to work. trying to get it to read backwards like a mirror without using the buffer class.

  public static void main(String[] args) {



   Scanner keyboard = new Scanner(System. in); 

   System.out.println("Enter a phrase:");

   String phrase = keyboard.nextLine();
   String Rphrase;

   int n = phrase.length();
   int r = 0;

   do{
        n--; r++;
    Rphrase[r] = phrase[n];

  }while(n >= 0);


   System.out.println(Rphrase);
axiopisty
  • 4,972
  • 8
  • 44
  • 73
  • 1
    There are multiple errors in the code you've posted. I highly recommend you remove everything and start over. This is simply done by 1.) reading in the `String` 2.) converting the `String` to an `array` of `chars` 3.) using a `for-loop` to iterate through the `char[]` backwards. – Tdorno Oct 25 '13 at 21:49
  • 1
    String rPhrase = new StringBuilder(phrase).reverse().toString(); – Akshaya Shanbhogue Oct 25 '13 at 21:50
  • 1
    @Tdorno: And what made you think he's using a library? – Sujay Oct 25 '13 at 21:50
  • It appears to be a class name! Rphrase is a variable here :D The importance of camelCasing :) – Akshaya Shanbhogue Oct 25 '13 at 21:52
  • @Sujay Ah, my bad Rphrase shows incorrectly because he capitalized it. But the approach is still awful. =D – Tdorno Oct 25 '13 at 21:53
  • Thank you all for the help and advise. I see that its evident to all of you that i have only been coding for a month lol. Ill make sure to camelCase my variables for now on. It looks like i was trying to call on the string as if it were an array not a string. Its fixed now. Thanks again! – learningCurve Oct 26 '13 at 00:15

6 Answers6

1

I have provided 4 ways of getting the output of the String reversed.

Option 1:

Just iterate the String backwards.

for (int i=phrase.length()-1; i>-1; i--) { 
    System.out.print(foo.charAt(i));  
}

Option 2:

If you would like to put it in the other buffer you can do:

char[] buffer = new char[phrase.length()];
index = 0;
for (int i=phrase.length()-1; i>-1; i--) { 
    buffer[index++] = foo.charAt(i);  
}

Option 3:

You said you didnt want to use the buffer class (which I think you're referring to StringBuffer so I'm assuming you dont want to use StringBuilder either) so here is how you can do it strictly with Strings (which is rather inefficient, because a new String is constructed each iteration):

String foo = "";
for (int i=phrase.length()-1; i>-1; i--) { 
    foo += foo.charAt(i);  
}

Option 4:

A most likely more efficient way of doing this though, is by using StringBuilder:

StringBuilder sb = new StringBuilder(foo.length());
for (int i=foo.length()-1; i>-1; i--) {
    sb.append(foo.charAt(i));
}
String reverse = sb.toString();

OR

Refer to this for very simple String reversal with a StringBuilder:
Reverse a string in Java

Community
  • 1
  • 1
Tyler
  • 827
  • 2
  • 11
  • 25
0

Try:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System. in); 

    System.out.println("Enter a phrase:");

    String phrase = keyboard.nextLine();
    String rPhrase = "";
    for (int i = phrase.length() - 1; i >= 0; i--)
        rPhrase += phrase.charAt(i);
    System.out.println(rPhrase);
}
sve
  • 4,336
  • 1
  • 19
  • 30
  • 1
    Wouldn't it be better to let the OP figure out the problem and then fix the problem on his own? Rather then just doing it for him? – Tdorno Oct 25 '13 at 21:54
  • 1
    If you're going to write it for him, don't keep the `Rphrase` variable name, always start variables with lower case letters. – BlackBox Oct 25 '13 at 21:55
  • @Tdorno I guess so.. But it's too late, sry :( – sve Oct 25 '13 at 21:56
0

This is what you need.

String reverse = "";
String toReverse = "hello";

for(int i = 0; i<toReverse.length();i++){
    reverse += toReverse.substring(i,i+1);
}

System.out.println(reverse);
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
0

Some hints, rather than a complete solution...

Your loop will run for one more iteration when n = 0, which will lead to trying to access index -1. So perhaps try n > 0 as your condition.

And what would happen if the string is empty? It would also try to access index -1, before ever getting to the loop. Perhaps you should put the condition at the beginning.

String doesn't support the [] operator - try:

Rphrase += phrase.charAt(n);

In which case you may as well get rid of r.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

you have to make a decrement so it can read its value back to front

for (int i = name.length() - 1, j = 0; i >= 0; i--, j++) {
newName[j] = name.charAt(i); enter code here

}

    System.out.println(newName); 

}

0

Simply use StringBuilder.reverse

str = new StringBuilder(str).reverse().toString();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275