In Java, how can I manipulate a variable instantiated from another class?

107 views Asked by At

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!

3

There are 3 answers

0
Francisco Romero On BEST ANSWER

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 class Player making reference to the method substring (of the class String as I put above) and you should have to make reference to some String from your class Player. Maybe it could be, supposing that your class Player has the name of the player and the number in the game (it's an example), like this:

public class Player
{
   int numPlayer;
   String name;
}

and with your methods GET and SET for this 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 to the GET method that returns your String and then you will be able to use substring method of String class. Something like this:

int playerdigit = Integer.parseInt(number.getName().substring(i,i+1));

Also, I saw that you are trying to make:

number.getGuess();

Maybe what you are trying (I don't know that) it's to get a String with getGuess method. Then you should do that:

String guess = number.getGuess();

to store the value of your String.

After, you will be able to make the substring:

int playerdigit = Integer.parseInt(guess.substring(i,i+1));

I expect it helps to you!

0
twentylemon On

Presumably your Player class has no method called substring. Maybe you're missing some call like number.getMyStringMember().substring(i,i+1)?

1
Anurag Baddam On

The 'substring' method is a non-static method of the String class (non-static means that you call the method using an instance of the class rather than using the class name itself). Therefore you want to call this method in the format: some_string.substring(start_index, end_index), keeping in mind the ending index is not inclusive. In your case, you are trying to call the substring method on a player object (since your 'number' variable is bound to a player object, and the Player class does not have a substring method). Instead, you probably should do the following (which has better naming convention as well since "number" does not refer to a player object):

Player p = new Player();
String number = p.getGuess();

Then, in your loop, you can do:

Integer.parseInt(number.substring(i,i+1));

and check each digit individually with your computer's generated number.

Hope this helps!