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!

Can you explain the problem(s) you are having? Does the code compile? Does it execute?
Are the results incorrect?

Hi,

The code does not compile, and I have no clue about how to go about filling in the array.

If the user enters 6, I want to fill the array in up to 6. So 1, 2, 3, 4, 5, 6 but I have no clue how to format the for loop for this. I scrapped that version of the code and started from fresh because I had so much stuff in there that was confusing me. I know now have this as the new version:

// ----------------------------------
// 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[ ] )
	{
		
		// Declare the main array to be used in this homework
		int aintMainArray[ ];
		
		// Declare the variables needed for the following procedures
		int intArraySize = 0;
		aintMainArray = new int[ intArraySize ];
		
		
		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( "" );
		
	}
	
	
	
	
	
	// ----------------------------------------------------
	// Enter the size of the array and return it back to
	// the user
	// ----------------------------------------------------
	public static int ArraySize( )
	{
		
		// Define the variables needed for this subroutine
		Scanner inputArraySizeEntered = new Scanner( System.in );
		int intArraySize = 0;
		
		
		
		do
		{
			System.out.println( "Please enter (between 1 and 100) the size of the array..." );
			intArraySize = inputArraySizeEntered.nextInt( );
		}
		while ( intArraySize < 1 || intArraySize > 100 );
		
		return intArraySize;
		
	}
	
	
	
	
	
	// ----------------------------------------------------
	// Populate the array using the number which is stored
	// as the size of the array
	// ----------------------------------------------------
	public static int PopulateArray( )
	{
		
		// Define the variables needed for this subroutine
		int intIndex = 0;
		int intTotal = 0;
		
		for ( intIndex = 0; intIndex < intArraySize; intIndex += 1 )
		{
			intTotal = intIndex + intArraySize;
		}
		
	}
	
}

The code does not compile

Please copy the errors and paste them here.

I use the command prompt to compile, so here is the output:

F:\school\Java 1\programs\CHomework7>javac CHomework7.java
CHomework7.java:79: cannot find symbol
symbol  : variable intArraySize
location: class CHomework7
                for ( intIndex = 0; intIndex < intArraySize; intIndex += 1 )
                                               ^
CHomework7.java:81: cannot find symbol
symbol  : variable intArraySize
location: class CHomework7
                        intTotal = intIndex + intArraySize;
                                              ^
CHomework7.java:81: operator + cannot be applied to int,intArraySize
                        intTotal = intIndex + intArraySize;
                                   ^
CHomework7.java:81: incompatible types
found   : <nulltype>
required: int
                        intTotal = intIndex + intArraySize;
                                            ^
4 errors

cannot find symbol
symbol : variable intArraySize

Where is the variable intArraySize defined? It is not in scope where you are trying to use it. Move it out of the main method to make it a class variable so all methods can see it.

What is the loop at line 79 supposed to do? The value of intTotal could be computed by a single statement.

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.