Arrays- Showing the populated array.

Hey,

this is my first semester of Java, so it is all news to me.

My question Im sure isnt difficult to answer.

"Write a program that prompts the user for a value and creates an integer array of that size. Prompt for initial values to populate the array."

This is what I have so far:

import java.util.Scanner;

public class numbers

{

public static void main(String[] args)

{

int arrayLength;

int i;

int x = 0;

Scanner console = new Scanner(System.in);

System.out.print("Enter size of array: ");

arrayLength = console.nextInt();

int[] anArray = new int[arrayLength];

for (i = 1; i <= anArray.length; ++i)

{

System.out.print("Enter value #" + i + ": ");

anArray[x] = console.nextInt();

}

}

}

Now, I want to check if the numbers that I entered do in fact populate this array. How can I output them on the screen to check? Ive spent a few hours on this, my book doesnt cover it, it only covers arrays with a given population and given numbers such as "int[] anArray = {5, 10, 15, 20}". My for statement could be wrong also.

I would appreciate any feedback.

Thank you.

[1232 byte] By [HaloPolicea] at [2007-10-2 15:48:12]
# 1

You code uses anArray[x] but I think you mean anArray Also, arrays start from 0 so your for loop should start from 0.

Well, you wrote a for loop that fills the array. It goes like this: for each array index, prompt the user, get user input into the current array index.

To display the array, you can use similar logic: for each array index, display the current array index. The current array index value is anArray and displaying it is similar to displaying a prompt.

atmguya at 2007-7-13 15:49:40 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

> You code uses anArray[x] but I think you mean

> anArray Also, arrays start from 0 so your for

> loop should start from 0.

I think it is supposed to be anArray[x]. But, x needs to be incremented, and isn't right now. The 'for' loop is 1..anArray.length so that it asked for #1, #2, #3, ...

Or, don't change the 'for' loop and use:

anArray[i-1]

MLRona at 2007-7-13 15:49:40 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...