Just to clarify, we were given a pre-existing template that already had the array named "array" with an array size of 10, and my homework was to rewrite the existing file to make the array size be specified by the first command-line argument but if no command-line arg is supplied, to leave 10 as the default size (that's the reason I used the If and Else statements on lines 26-32)) Just an FYI, the original template I was given is below as well as my revised code. I've been continuing to work on this all day, but I'm still getting errors (lines 28 and 32).
Original code:
CODE
public class InitArray
{
public static void main( String args[] )
{
int array[]; // declare array named array
array = new int[ 10 ]; // create the space for array
System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
// output each array element's value
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
} // end main
} // end class InitArray
My revised code:
CODE
import java.util.Scanner; // program uses class Scanner
public class InitArrayNew
{
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in);
// declarations
int array[]; // declare array named array
// prompt for user to input a number for the array size
System.out.println( "Please enter a number for the array size: " );
array = new int[ input.nextInt() ]; // create the space for array and input number
int num = input.nextInt();
int inputNumber = 0; // number value entered
// array = new int[ inputNumber ]; // create the space for array and
// validate the input
if (inputNumber > 0)
{
array[num] = array[length];
}
else
{
array[10] = array[length];
}
System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
// output each array element's value
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
} // end main
} // end class InitArrayNew