Creating an Array help


#1

Hi,
I am working on a class assignment but I am stuck.
The assignment is

  1. Create an int array called salesFigure that will hold 5 elements.
  2. Use a for loop to print all the elements. (They should all be 0 since no information has been assigned.)
  3. Assign the 5 elements in the array by index location starting at index 0 and through index 4 in this order: (250, 1000, 2500, 4500, 3300)
  4. Print the array using a for loop

Here is what I have.
int[] salesFigures = new int {250, 1000, 2500, 4500, 3300};
numbers[0] = 250;
numbers[1] = 1000;
numbers[2] = 2500;
numbers[3] = 4500;
numbers[4] = 3300;

for (int i = 0; i <salesFigures; i++)

{
System.out.println(salesFigures[i]);
}

I get an error of
expecting DOT, found “salesFigures”

What am I missing?
THanks


#2

Hi Awal, you have forgot the size method:

for (int i = 0; i <salesFigures.size(); i++)

Keep us up to date!


#3

Thank you Dennis, I made the change but it still is throwing an error. Also where would I know the size based on the directions?
int[] salesFigures = new int{250, 1000, 2500, 4500, 3300}; // THIS IS WHERE THE YELLOW IS
numbers[0] = 250;
numbers[1] = 1000;
numbers[2] = 2500;
numbers[3] = 4500;
numbers[4] = 3300;

for (int i = 0; i <salesFigures.size(); i++)

{
System.out.println(salesFigures[i]);
}


#4

Hops… I miss:

for (int i = 0; i &lt;salesFigures.length; i++)

Sorry but I’m writing by smartphone and I not tryed it…


#5

Thanks Dennis, I am still getting errors. Sad part is this is only the first half of the assignment. This is going to take me forever.


#6

There is a tutorial on arrays

See Website

Since (2) requieres 0 output for all members of the array, shorten your first line before the =

Then for loop with println

Then assign values (you have those 5 lines already)

Then again do the println

Dead easy


#7

Dennis, I got it!!! now on to the next part.

Thanks for your help. I may hit you up again if that’s ok.


#8

Thanks Chrisir,
I figured it out by watching the a Youtube on Arrays.

Thanks


#9

Very good!
Thanks for letting me know…