-6
public class ggg
{
static int y=0;
static int x;

static String h;
public static void main(String [] args)
{

    String s = "hadoyef";
    x = s.length();
    System.out.println(s);
    reverse(s);
    System.out.println(s);
}

public static String reverse(String s){

    if (s.length() == 1){
        //System.out.print(s);
        h = h + s.substring(0,1);
        s=h;
        System.out.println(s);
        return s;
    }
    else{
        h = h + s.substring(s.length()-1,s.length());
        return reverse (s.substring(0, s.length()-1));  

        //System.out.print(s.substring(0,1));

    }
}
}

Please help me I dont understand why the s=h; part isn't working. Ignore from here its making me post more detail and idk what to say so im just going to ramble until it works thanks for whoever helps.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
frank
  • 7

3 Answers3

5

When you call reverse(s); in main, you're not assigning the result to anything.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • @frank You're only calling reverse once in main. You need to print the value returned from it instead of printing `s` again. – Bill the Lizard Apr 08 '13 at 22:49
  • @frank Its a scope issue. you are saying `s=h` in reverse method which is getting confined to its `String s` variable which is getting collected in method. This is not going to make any impact on `String s` in main. To see effect use `System.out.println(reverse(s));` in your main method. – Smit Apr 08 '13 at 22:51
  • thank you so much for explaining this to me, smit – frank Apr 08 '13 at 23:01
3

You need 2 changes in your code.

  1. Initialize static String h as:

    static String h = "";
    
  2. Use the return value in main method as:

    s = reverse(s);
    
Beta
  • 96,650
  • 16
  • 149
  • 150
prashant
  • 1,805
  • 12
  • 19
1

because you are not using result of method reverse(s); why you are returning String when it`s not used ?

Martin V.
  • 3,560
  • 6
  • 31
  • 47