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

2017/2/21

Using int array values in other classes

Smug-iwnl- Smug-iwnl-

2017/2/21

#
When I try to use an int array from my world in an actor class, I get an error that java.lang.String cannot be converted to int. Here is my code:
In World:
public Integer [] stateVotes={55,4,3,4,11,18,29};

In class:
wash.changeVotes(stateVotes[0]);
The changeVotes method requires an int.
Super_Hippo Super_Hippo

2017/2/21

#
//in world:
private int[] stateVotes={55,4,3,4,11,18,29};

public int getStateVotes(int state) {return stateVotes[state];}
guessing that "class" is an actor in the world above: (Change MyWorld to your world's name.)
wash.changeVotes(((MyWorld)getWorld()).getStateVotes(0));
davmac davmac

2017/2/21

#
We need to see more code, I think. I don't see anything wrong with what you've posted (except that stateVotes probably refers to a different variable in both cases).
You need to login to post a reply.