This discussion is archived
10 Replies Latest reply: Aug 20, 2010 8:36 AM by JosAH RSS

Help needed regarding "recursive method execution"

843789 Newbie
Currently Being Moderated
Hi everybody,m a beginner ,while trying to execute the following code in the output i can't getthe symbol ( ' ) correctly printed ,plz lemme know where m i at fault-

class Song{
public static void lyrics(int n){

if(n==1){
System.out.println("No bottles of beer on the wall,; no bottles of beer, ya' can?t take one down,ya' can?t pass it around, 'cause there are no more bottles of beer");
}
else{
System.out.println(n+" bottles of beer on the wall," + n + "bottles of beer, ya? take one down, ya?pass it around," + (n-1)+" bottles of beer on the wall.");
lyrics(n-1);
}

}
public static void main(String[] args)
{
lyrics(3);
}
}

Edited by: basant1988 on Aug 18, 2010 2:26 AM

Edited by: basant1988 on Aug 18, 2010 2:37 AM
  • 1. Re: Help needed regarding "recursive method execution"
    JosAH Newbie
    Currently Being Moderated
    Who cares about those quotes? There's a bottle of beer missing!

    kind regards,

    Jos ;-)
  • 2. Re: Help needed regarding "recursive method execution"
    PhHein Guru Moderator
    Currently Being Moderated
    Don't multipost! I've deleted your other thread.
  • 3. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    When you say that you can not get the quotes printed properly what do you mean?
    Is it that none of them are displayed or that there are too many or they are in the wrong place or what?

    Also, you will have to repost your original source code using the
     tags, the code you posted was reformatted by the forum software. 
    
    Just press the *CODE* above the text entry area and paste your original source betwen the tags.  Note that I said original the code in the post is already messed up.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 4. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    That is because ' needs an escape character. Google "java escape character" and learn all about it.

    Also, this is a really poor example of recursion. This could have been done much easier and better even using a simple loop. If this is something you came up with to turn in as an example of recursion, I would start over. While technically you are using recursion, there is no reason for it and recursion should never be used as a replacement for the loop, which is exactly what you are doing here.

    Finally, if you want to receive help here, there are a few things you can do to not tick off the people you are asking to help. An example that is excusable since you are new here is not using code tags when posting code. Using code tags makes the code much easier to read, thus making people much more willing to read it. But the unexcusable error you have made is using lazy type. Does it really hurt you to type "am" instead of "m", I mean it is like one extra keystroke? You used "m" twice, once for "I'm" and once for "am", and you used "plz" for "please". Now there are is one of two ways this can be taken, either you don't care enough to respect the professionals volunteering their time to help you here by giving slightly more effort on your part to completely spell out the words, or you are allergic to vowels. Since you use vowels in other words, it leaves us only with the first possibility. We are not paid to help you, the least you can do is show a little respect. Your request was an easy one, and I am sure several posters who could have easily answered it simply skipped over it because of the lazy type, I myself would have likely explained it myself instead of telling you what to google if your post hadn't rubbed me wrong.

    Just saying!

    JSG
  • 5. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    Thanks for giving your time to my problem.The actual problem is when i execute the code the apostrophe symbol after "ya" and elsewhere in the code doesn't correctly appear in the output rather some strange symbol does appear.
    class Song{
    public static void lyrics(int n){
    
    if(n==1){
    System.out.println("No bottles of beer on the wall,; no bottles of beer, ya' can?t take one down,ya' can?t pass it around, 'cause there are no more bottles of beer");
    }
    else{
     System.out.println(n+" bottles of beer on the wall," + n + "bottles of beer, ya? take one down, ya?pass it around," + (n-1)+" bottles of beer on the wall.");
    lyrics(n-1);
    }
    
    }
    public static void main(String[] args)
    {
    lyrics(3);
    }
    }
  • 6. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    getting one more bottle is easy for me but its not same getting that symbol.;-)thanks
  • 7. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    Thanks for giving your precious time to my problem.I never meant to hurt anybody or waste someone's time,neither i know who is paid and who is not.I would never make mistakes knowingly just because someone is getting paid.But if i unknowingly did something wrong am sorry.
    I used recursion because it was a question somewhere to use recursion for this and in the output at some places i got the symbol printed and at some not.By the way m learning it since last two days so forgive me for some silly mistakes in my codes.
    class Song{
    public static void lyrics(int n){
    
    if(n==1){
    System.out.println("No bottles of beer on the wall,; no bottles of beer, ya' can?t take one down,ya' can?t pass it around, 'cause there are no more bottles of beer");
    }
    else{
     System.out.println(n+" bottles of beer on the wall," + n + "bottles of beer, ya? take one down, ya?pass it around," + (n-1)+" bottles of beer on the wall.");
    lyrics(n-1);
    }
    
    }
    public static void main(String[] args)
    {
    lyrics(3);
    }
    }
  • 8. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    I think your problem is that you are using two different characters one is U+0027, the apostrophe.
    The other is the right single quotation mark. I think its unicode point is U+2019.

    My guess is you want to use the apostrophe.
  • 9. Re: Help needed regarding "recursive method execution"
    843789 Newbie
    Currently Being Moderated
    [java tutorial|http://download-llnw.oracle.com/javase/tutorial/java/data/characters.html]

    Check out the escape sequences section in the link to the tutorials above.

    You are trying to use a single quote inside double quotes. To do this you must use an escape sequence. Well either an escape sequence or unicode, but the escape sequence is much easier.

    incase you still don't get it, which means you probably still haven't looked at the link, replace this
    "bottles of beer, ya? take one down, ya?pass it around,"
    with this
    "bottles of beer, ya\? take one down, ya\?pass it around,"
    Notice the difference? That is called an escape sequence, now do it in the other line with single quotes and problem solved, well this problem anyways.

    JSG
  • 10. Re: Help needed regarding "recursive method execution"
    JosAH Newbie
    Currently Being Moderated
    I really don't understand all the escape stuff here: single quotes don't need to be escaped in double quotes (a String), nor do double quotes have to be escaped in single quotes (a char); have a look:
    System.out.println("'"+'"'+'\''+"\"");
    kind regards,

    Jos