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 classPlayermaking reference to the method substring (of the classStringas I put above) and you should have to make reference to someStringfrom your classPlayer. Maybe it could be, supposing that your classPlayerhas the name of the player and the number in the game (it's an example), like this:and with your methods
GETandSETfor this attributes:You should make reference to the
GETmethod that returns yourStringand then you will be able to usesubstringmethod ofStringclass. 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
StringwithgetGuessmethod. 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!