I am very new to Java and am making a simple application. That consists of two classes, Computer
and Player
. In Player
, I prompt the user to guess a five digit string. In Computer
, I instantiate the Player
class as so:
Player number = new Player();
number.getGuess();
Next, I created a loop to check if the computer and player's numbers match. In order to do this, I need to turn the five digits of the player's number into a integer. Why won't Java let me do:
int playerdigit = Integer.parseInt(number.substring(i,i+1));
It keeps giving me the error that the method .substring isn't defined. How can I do this? Any help is appreciated!
It is because you are trying to access to
substring
(a method of String class). In the code that you show to us you are trying to access to a method in your classPlayer
making reference to the method substring (of the classString
as I put above) and you should have to make reference to someString
from your classPlayer
. Maybe it could be, supposing that your classPlayer
has the name of the player and the number in the game (it's an example), like this:and with your methods
GET
andSET
for this attributes:You should make reference to the
GET
method that returns yourString
and then you will be able to usesubstring
method ofString
class. Something like this:Also, I saw that you are trying to make:
Maybe what you are trying (I don't know that) it's to get a
String
withgetGuess
method. Then you should do that:to store the value of your
String
.After, you will be able to make the substring:
I expect it helps to you!