Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: "Blastoff!" Recursion Dilemma

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Location
    The Batcave, USA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow "Blastoff!" Recursion Dilemma

    Hello Friends,

    I have kept a very low profile on this forum, but for the past few weeks I have been reading as much about Java as I could. This is a language that I want to master, but in the process of creating a recursion, I can't seem to debug it so that it will compile successfully. For those of you who are familiar, this is a pretty popular example of a recursion called "Blastoff!" Here's what I have:

    public static void main(String[] args) {
     
    countdown(3);}
     
    class countdown {
     
    public static void countdown(int n) {
    		if (n == 0) {
    		System.out.println("Blast off!!!");
    		} else {
    		System.out.println(n);
    		countdown(n-1);
    }}}}

    When I compile it, it says that I have three errors. They all read "class, interface, or enum expected."

    Error I - Line 1

    public static void main
    ^

    Error II - Line 3

    countdown(3);}
    ^

    Error III - Line 13

    }}}}
    ^

    So being the diligent student that I am, I decided I would research the problem before I came whining to you guys about it. I figured it was a popular program and I copied it directly out of the book, so surely someone else has this problem. This is what I found:

     
    class Blastoff {
     
        public static void main(String[] args) {
            countdown(Integer.parseInt(args[0]));
        }
     
        private static void countdown(int n) {
            if (n == 0) {
                System.out.println("Blastoff!");
            } else {
                System.out.println(n);
                countdown(n - 1);
            }
        }
    }

    This probably works, but I don't understand what is meant by" {countdown(Integer.parseInt (args[0]));} ." I read the original author's attempt to explain it, but I don't understand it well enough to see if it accomplishes the purpose of the assignment - which is to effectively write a recursion.

    What I'm trying to do
    What I'm trying to do is make countdown a class that is declared an integer value of three, and execute that class in main. In the execution of this class, the method possesses a nested conditional that invokes itself (a recursion). If it is the condition that n !=0, it's supposed to System.out.println the value of n and subtract 1 unit from the declared integer (3) each time it invokes itself until the condition that it reaches 0. When it reaches 0, System.out.println "Blast off!!!"

    Can anybody give me a hand with this? I've looked at it for about two hours now and can't seem to debug properly.


    EDIT - I fixed the third error by removing the extra bracket on line 13.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: "Blastoff!" Recursion Dilemma

    Phew! I'm glad that your second section of posted code is formatted much better than the first. Please use the second as a model and pretend you've never seen the first.

    The statement you asked about is confusing to a new Java programmer, but it's irrelevant to the countdown() method or the subject of recursion. You could rewrite the program replacing that line with:

    countdown( 3 );

    which would be more equivalent to your original.

    Rather than try to explain the line you asked about to you, I suggest you refer to the Oracle Tutorials and the API for help. If you learn to do this yourself, then you're well on your way to figuring out future Java mysteries rather than having to ask someone to explain them to you.

    For the Integer.parseInt() part, refer to the Integer API and read what the parseInt() method does. For the parameter args[0], read the Oracle tutorials on arrays and Command Line Arguments. I provided you with a link to the last, because I wasn't sure you'd know what to search for. The rest you should be able to find.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    The Batcave, USA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Blastoff!" Recursion Dilemma

    Greg,

    Thank you very much for your help, the program now works! It looks like this:

     
    class countdown {public static void main(String[] args) { countdown(3);}
     
    public static void countdown(int n) {
    	if (n == 0) {
    		   System.out.println("Blast off!!!");
    	} else {
    		   System.out.println(n);
    		   countdown(n-1);
    }}}
    Last edited by Wrathbat; July 29th, 2014 at 05:57 PM. Reason: Fixed

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: "Blastoff!" Recursion Dilemma

    Yes, but please format the code using the second program you posted as a guide. The code in post #3 is U-U-Ugly.

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. My recursion is "leaking"...
    By raginpirate in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 4th, 2013, 06:44 PM
  5. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM

Tags for this Thread