instantiation - In Java, how can I manipulate a variable instantiated from another class? -
i new java , making simple application. consists of 2 classes, computer
, player
. in player
, prompt user guess 5 digit string. in computer
, instantiate player
class so:
player number = new player(); number.getguess();
next, created loop check if computer , player's numbers match. in order this, need turn 5 digits of player's number integer. why won't java let me do:
int playerdigit = integer.parseint(number.substring(i,i+1));
it keeps giving me error method .substring isn't defined. how can this? appreciated!
it because trying access substring
(a method of string class). in code show trying access method in class player
making reference method substring (of class string
put above) , should have make reference string
class player
. maybe be, supposing class player
has name of player , number in game (it's example), this:
public class player { int numplayer; string name; }
and methods get
, set
attributes:
public void setnumplayer(int numplayer) { this.numplayer = numplayer; } public int getnumplayer() { return numplayer; } public void setname(string name) { this.name = name; } public string getname() { return name; }
you should make reference get
method returns string
, able use substring
method of string
class. this:
int playerdigit = integer.parseint(number.getname().substring(i,i+1));
also, saw trying make:
number.getguess();
maybe trying (i don't know that) it's string
getguess
method. should that:
string guess = number.getguess();
to store value of string
.
after, able make substring:
int playerdigit = integer.parseint(guess.substring(i,i+1));
i expect helps you!
Comments
Post a Comment