Complete Program
Notice that the two for loops are the same.
This is very common.
This program can be "copied-pasted-and-run" using NotePad and the JDK.
When you do this, change the "5" in the array constructor and note
the effect.
import java.io.* ;
class inputArray
{
public static void main ( String[] args ) throws IOException
{
int[] array = new int[5];
int data;
BufferedReader inData = new BufferedReader ( new InputStreamReader( System.in ) );
// input the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "enter an integer: " );
data = Integer.parseInt( inData.readLine() );
array[ index ] = data ;
}
// write out the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "array[ " + index + " ] = " + array[ index ] );
}
}
}
|
|