0

hi :)
This program requires me to read user input from the commandline (a int and a string) and use recursion to print out the the first letter of the string x times based on the int user input:

I have this so far, but it seems print out just one instance...it doesn't seem to be looping up to the for statement

public class test
   {
     /*******************************************************************************
     *  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
            String word = commandlineArguments[1];
         	         
            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
               }
                  
            String outputString; // declares String
            Integer outputInt; // declares Integer
         
            outputString = printString(number, word); // calls printStars method
            System.out.println( outputString ); //prints printStars method
}
}

 public static String letter( int start, String oneWord ) 
      {
         
         if( start == 0 ) // if start = 0, then result is 0.
         {
            return "0";
         } 
         else
         {
            oneWord = oneWord.substring(0,1);
         
            for (int i = 0; i < start; i++)
            {
               oneWord = oneWord;
            }
            return oneWord;
         
           
             
         } 
      
      
      }

Thank you in advance for you help!! :)

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

Line 51 - You're assigning a string to itself. What's the point?

Lines 49 - 52 - Either use a loop or recursion. Not both. You are required to use recursion, so don't use a loop.

Line 47 - You are overwriting the parameter you passed to the function without ever using it, which defeats the purpose of passing a parameter.

Line 45 - This has no ill effect, but is unnecessary. There is no need for an "else" statement since there was a "return" in the "if" part.

Line 53 - You have no recursion. Only the base case returns to the main function. This isn't the base case. You need to return a call to the function that you are in. That's the definition of recursion.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.