Results 1 to 4 of 4
  1. #1
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Help with Recursion and return statement

    The program requires an integer input from a user at the command args and prints out the numbers backward. I have this so far, however, it only prints 1 number, which is the number entered into args. Note: all printouts must occur in main method.
    Java Code:
     public class KashiwabaraNicole7
       {
         /*******************************************************************************
         *  Initializes program
         *  @ param commandlineArguments
         ********************************************************************************/
        
           public static void main(String[] commandlineArguments) 
          {
             if (commandlineArguments.length == 0) {
                System.out.print("Error: You must enter at least 1 commandline argument ");
             } 
             else 
             {
                Integer number = new Integer(0); //initialize number
             
             
                try // check to verify if input is an integer
                {
                   number = Integer.parseInt(commandlineArguments[0]);
                } 
                    catch (NumberFormatException exception) // NumberFormatException
                   {
                      System.out.print(exception);
                      System.out.println(" is not an integer");
                      System.exit(1); // end program
                   }
                      
                Integer num = KashiwabaraNicole7.stars(number); //A (return address)
                System.out.print(num+",");
              
                  
             }
          }
       
        /**
       * Writes a character string backward using recursion.
       * @param string a character string
       * @returns a backwards string
       */
           public static int stars(Integer number) 
          {
            
             String asterisk = "*";
               
            
                for(int i= number; i>=0;i--)
             {
    			  return i;			  
    
                }
               
             
             return number;
          
          } 
           
       
       
       }//end of class

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Look at your stars method(why is it called stars?). It doesn't write any strings backward. It simply declares a string called asterisk and does nothing with that string.
    The for loop is also virtually useless. You just return the number that was passed in.
    There is also no recursion whatsoever in your code.

  3. #3
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    whoops! I'm also supposed to print out the same amount of asterisks as the user input integer. How do I write it so that is is recursive? What do I need to do? Sorry, but this is my first time writing a recursive program, where the printout needs to occur in the main method. :o

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Why do you want to use recursion to reverse a string? Is that part of your assignment requirements? If you have to print out as many stars as the number of characters then don't mix it with the reversing part. Your methods must be well focused on their goal and not try to do everything (cohesion).

Similar Threads

  1. insert statement return problem in ibatis
    By dheerajsea123 in forum JDBC
    Replies: 1
    Last Post: 05-20-2010, 10:22 AM
  2. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 AM
  3. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  4. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  5. [SOLVED] return statement
    By Nakira in forum New To Java
    Replies: 8
    Last Post: 11-12-2008, 11:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •