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.

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

Recommended Answers

All 4 Replies

The program requires an integer input from a user at the command args and prints out the numbers backward.

What is your exact Requirement?

if you enter 10, it should print,

10, 9, 8, 7, ...

or

01

Which one you want?

If anything else, Please clarify me.

I think you need the first option of out put,

like,

10, 9, 8, ...

Check Here

public static int stars(Integer number) 
      {
        
         String asterisk = "*";
           
        
            for(int i= number; i>=0;i--)
         {
			  return i;			  

            }
           
         
         return number;
      
      }

int the above code,

for(int i= number; i>=0;i--)

if value of number is 10, The value of i becomes 10

return i;

and then it returns the value of i i.e. 10 in our case.

Whenever we use the return statement it returns the control form the point where it encountered the return statement to the place from where the method is got called.

So in your case,

In the first iteration of the loop i.e. when the value of the i is 10 it return the value 10 and quits the method. It will never come back to the method or the loop. The loop will be terminated there itself.

So that you are getting only one number that is the number you have entered. In our case the number is 10.

If you have any other problem please reply soon.

regards.

This is what is required.
1. Write a Java application that executes the following 5 recursive methods, each of which returns a value, and prints that value in the main() method, where X is a positive integer on the commandline.
1. Method 1: Return and print in main() a row of X asterisks (****...)
2. Method 2: Return and print in main() the numbers from X to 0 (X, ..., 4, 3, 2, 1, 0) by returning the numbers as a String.
3. Method 3: Return and print in main() the numbers from 0 to X incrementing by 1 (0 + 1 + 2 + 3 + 4 + ... + X) by returning the numbers as a String.
4. Method 4: Return and print in main() the sum of the numbers from 0 to X incrementing by 1 (0 + 1 + 2 + 3 + 4 + ... + X)
5. Method 5: Return and print in main() the sum of the powers of 2 up to the Xth power of 2 by multiplying by 2 and adding the numbers together (1 + 2 + 4 + 8 + 16 + ... + 2X)
2. Use the commandline arguments to enter one positive integer argument.
1. X is the input for your methods.
2. You may find it useful to use more than one parameter for some of your methods.
3. At the beginning of the main method, check to make sure that the user entered the correct input.
1. If exactly one (1) positive integer argument is not entered on the commandline, then display an error message and end the program.
2. You also need to have a try-catch statement just in case a non-integer is inputted as a commandline argument.

Output

1. Example output for commandline argument 1:

*
1, 0,
0, 1,
1
3


2. Example output for commandline argument 5:

*****
5, 4, 3, 2, 1, 0,
0, 1, 2, 3, 4, 5,
15
63


3. Example output for commandline argument 10:

**********
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
55
2047

What can i do to make it loop back up to the for statement and print out each 'i' as it decreases?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.