This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kwhyte
Kwhyte wrote ...

2014/5/29

"Array required, but [Class Name] found"- Help!

Kwhyte Kwhyte

2014/5/29

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void createPrize()
    {
        for (int i = 0; i < 10; i++)
        {
            Prize prizes = new Prize(prizes[(i++)]);
            addObject(prizes, locationsX[i], 0);
        }
        for (int i = 0; i < 10; i++)
        {
            Prize prizes = new Prize(prizes[(i++)]);
            addObject(prizes, locationsX[i], 45);
        }for (int i = 0; i < 10; i++)
        {
            Prize prizes = new Prize(prizes[(i++)]);
            addObject(prizes, locationsX[i], 75);
        }
 
    }
I am using an array to put various images on the background for the players to "eat". The class of the images is "Prize" and the name of the array is "Prizes". Here are my arrays:
1
2
3
private String[] prizes =
        {"1344205930.jpg", "gold-number-5.png", "etilize_73890.png", "BLACK.png", "LARGEIMAGE_647491.jpg"};
    private int[] locationsX = {32, 96, 160, 224, 288};
My problem is when I compile, I get an error that highlights the "i++" part of my for loop saying that "Array required, but Prize found". Any ideas on what I did wrong, and how to fix it? Anything is greatly appreciated.
erdelf erdelf

2014/5/30

#
well.. you have the variable prizes for a String array, but you coded that it would later be seen as a Prize object rewrite the lines
1
Prize prizes = new Prize(prizes[(i++)]);
to
1
Prize prize = new Prize(prizes[(i++)]);
and of course the following line, change prizes to prize i am not quite sure what, the i++ part does here but ok btw. you will get an out of Boundaries error when executing this, because u ask for the tenth position in the locationsX array, but only have 5
Kwhyte Kwhyte

2014/5/30

#
Thank you so much for your help! I changed it from "prizes" to "prize" and got rid of the ++ after "i" and changed 10 to 5. However, now it is saying "constructor Prize in class Prize cannot be applied to given types;". Any ideas on what is wrong now?
erdelf erdelf

2014/5/30

#
give the constructor of the Prize class
You need to login to post a reply.