Hello,
I'm trying to figure out how to populate an array by using subroutines. We have begun procedures last week, and now this week we are moving onto arrays. College moves too fast sometimes. :(
What I'm trying to do is make a subroutine which will populate the entire array with the value read from the user. The value read from the user is stored in the variable named "intArraySizeEntered". I then have to pass the array as a parameter to the subroutine.
The following is what I have so far:
// ----------------------------------
// Name: John Mollman
// Class: Java 1 - 5271
// Abstract: CHomework7
// ----------------------------------
// Imports ----------------
import java.util.Scanner;
import java.io.*;
public class CHomework7
{
public static void main( String astrCommandLine[ ] )
{
int intArraySize = 0;
int aintNumbers[ ];
aintNumbers = new int[ intArraySize ];
int intPopulatedArraySize = 0;
System.out.println( "Step 1: Array Size" );
System.out.println( "----------------" );
intArraySize = ArraySize( );
System.out.println( "The array size is: " + intArraySize );
System.out.println( "" );
System.out.println( "" );
System.out.println( "" );
System.out.println( "Step 2: " );
System.out.println( "----------------" );
intPopulatedArraySize = PopulateArray( );
System.out.println( " " + intArraySize );
System.out.println( "" );
System.out.println( "" );
System.out.println( "" );
}
// ----------------------------------------------------
// Enter the size of the array and return
// it to the console.
// ----------------------------------------------------
public static int ArraySize( )
{
Scanner inputArraySizeEntered = new Scanner( System.in );
int intArraySizeEntered = 0;
int intArraySize = 0;
do
{
System.out.println( "Please enter (between 1 and 100) the size of the array..." );
intArraySizeEntered = inputArraySizeEntered.nextInt( );
}
while ( intArraySizeEntered < 1 || intArraySizeEntered > 100 );
return intArraySizeEntered;
}
// ----------------------------------------------------
// Populate the array with the value read from the user
// ----------------------------------------------------
public static int PopulateArray( )
{
int aintPopulatedArraySize[ ];
int intPopulatedArraySize;
aintPopulatedArraySize = new int[ intArraySizeEntered ];
int intIndex = 0;
for ( intIndex = 0; intIndex < aintPopulatedArraySize.length; intIndex += 1 )
{
aintPopulatedArraySize = aintPopulatedArraySize[ intIndex ];
}
return intPopulatedArraySize;
}
}
Thank you for your time!