The two lines:
data = Integer.parseInt( inData.readLine() ); array[ index ] = data ;
can be replaced by the single line:
array[ index ] = Integer.parseInt( inData.readLine() );
And then the declaration int data;
should be removed.
An array object is constructed as the program runs. The size of the array can be specified in a variable. Here is the previous example, with some modifications:
import java.io.* ;
class InputArray
{
public static void main ( String[] args ) throws IOException
{
BufferedReader inData =
new BufferedReader ( new InputStreamReader( System.in ) );
int[] array;
// determine the array size and construct the array
System.out.println( "What length is the array?" );
int size = Integer.parseInt( inData.readLine() );
array = new int[ _____________ ];
// input the data
for ( int index=0; index < array.length; index++)
{
System.out.println( "enter an integer: " );
array[ index ] = Integer.parseInt( inData.readLine() );
}
// write out the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "array[ " + index + " ] = " + array[ index ] );
}
}
}