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

2018/5/4

Help with arrays?

Astralman Astralman

2018/5/4

#
I just started learning arrays and I'm terrible at it! As you can see, I'm doing a coin toss game and I have to make sure the money value corresponds to the coin. Nickle = 0.05, Dime = 0.10, and Quarter = 0.25.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main (String[] args){
    double coinsArray[] = {0.05, 0.10, 0.25};
    String coinName[] = {nickle, dime, quarter};
    int coin;
    String nickle;
    String dime;
    String quarter;
 
 
 
    Coin coinArray[] = new Coin[2];
    Coin nickle[] = new Coin();
    coinArray[0] = nickle;
    Coin dime[] = new Coin();
    coinArray[1] = dime;
    Coin quarter[] = new Coin();
    coinArray[2] = quarter;
}
danpost danpost

2018/5/4

#
Since you are not using any args values in the method itself, I will presume no input is required for your program. Lines 1 and 2 create 2 arrays -- one of type double and the other of type String, both of which is assigned values for their elements. The next 4 lines declare one int and 3 String type fields. No problem so far (as best as I can tell). Now, line 11 creates an array of Coin type objects of length 2 of which a null-filled array is assigned. Line 12 creates another Coin type array, which is not assigned an array, but a simple Coin object, which you cannot do. If you just want to create a Coin object and assign it a variable named nickel, drop the array brackets on that line. Same with lines 14 and 16. Line 17 is problematic in that your coinArray array is only of length 2 and you are trying to assign a third object to an element that does not exist. Adjust line 11 to allow 3 elements in the array. Apparently, the Coin objects do not retain their own values. I wonder why this is the case.
Astralman Astralman

2018/5/4

#
I adjusted line 11. I thought it went 0,1,2 . But I guess that's not how it works. This is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main (String[] args){
    double coinsArray[] = {0.05, 0.10, 0.25};
    String coinName[] = {nickle, dime, quarter};
    int coin;
    String nickle;
    String dime;
    String quarter;
 
 
 
    Coin2 coinArray[] = new Coin2[3];
    Coin2 nickle = new Coin2();
    coinArray[0] = nickle;
    Coin2 dime = new Coin2();
    coinArray[1] = dime;
    Coin2 quarter = new Coin2();
    coinArray[2] = quarter;
Error Message I Got: C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:29: error: cannot find symbol String coinName = {nickle, dime, quarter}; ^ symbol: variable nickle location: class Coin C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:29: error: cannot find symbol String coinName = {nickle, dime, quarter}; ^ symbol: variable dime location: class Coin C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:29: error: cannot find symbol String coinName = {nickle, dime, quarter}; ^ symbol: variable quarter location: class Coin C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:38: error: variable nickle is already defined in method main(String) Coin nickle = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:39: error: incompatible types: String cannot be converted to Coin coinArray = nickle; ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:40: error: variable dime is already defined in method main(String) Coin dime = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:41: error: incompatible types: String cannot be converted to Coin coinArray = dime; ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:42: error: variable quarter is already defined in method main(String) Coin quarter = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:43: error: incompatible types: String cannot be converted to Coin coinArray = quarter;
danpost danpost

2018/5/4

#
I did not catch that one -- move lines 5 through 7 to before line 3 (can't use them before they are declared).
Astralman Astralman

2018/5/4

#
The cannot find symbol is gone, but what to do with the other errors? Variable already defined and String cannot be converted to Coin. Errors: C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:40: error: variable nickle is already defined in method main(String) Coin nickle = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:41: error: incompatible types: String cannot be converted to Coin coinArray = nickle; ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:42: error: variable dime is already defined in method main(String) Coin dime = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:43: error: incompatible types: String cannot be converted to Coin coinArray = dime; ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:44: error: variable quarter is already defined in method main(String) Coin quarter = new Coin(); ^ C:\Users\Rojas\Desktop\JavaPrograms\Coin\Coin.java:45: error: incompatible types: String cannot be converted to Coin coinArray = quarter;
danpost danpost

2018/5/4

#
Rename them. unless you intended only to have Coin2 objects and no String object (which I do not see anywhere); then, change all String in the code to Coin2 and remove Coin2 from lines 12, 14 and 16.
Astralman Astralman

2018/5/5

#
I still have errors :(
danpost danpost

2018/5/5

#
Astralman wrote...
I still have errors :(
Show what you have now. Same errors, or different? if not same, what?
Astralman Astralman

2018/5/5

#
Same errors.
You need to login to post a reply.