Getting a string value from within a method in another class
Okay, I'm kind of new to Java but I still know some stuff. I'm trying to get a string value from a separate class that is within a method in that class. I can't figure it out! I'm making a tic-tac-toe game ( Text-Based ).This is the code for the computer moving class.
Code :
public class compMove {
String compChoice;
public String move() {
String[] rowsComp = {"(1,1)", "(1,2)", "(1,3)", "(2,1)", "(2,2)", "(2,3)", "(3,1)", "(3,2)", "(3,3)"};
int rowsCompLength = rowsComp.length;
int rand = (int) (Math.random() * rowsCompLength);
compChoice = rowsComp[rand];
System.out.println(compChoice);
return ("compChoice");
}
}
This is the code for the spaces that can be selected by either an X or an O.
Code :
public class space {
String space11 = "-";
String space12 = "-";
String space13 = "-";
String space21 = "-";
String space22 = "-";
String space23 = "-";
String space31 = "-";
String space32 = "-";
String space33 = "-";
public void space11 () {
compMove computer = new compMove();
computer.move();
if(computer.move().compChoice.equals("(1,1)")) {
space11 = "o";
}
}
public void printSpace11() {
space11 ();
}
}
As you can see I am trying to check if the value of the string compChoice which gets its value in the method move in the compMove class is equal to (1,1). This code does not compile but can someone tell me how to fix this line of code to work?
Code :
if(computer.move().compChoice.equals("(1,1)"))
Please Help!
Re: Getting a string value from within a method in another class
Return the variable from the move method
Code java:
public String move() {
....
return compChoice;
}
....
String compChoice = computer.move();
if ( compChoice.equals("1,1") ){
}
Re: Getting a string value from within a method in another class
Ok, there are a few things to note about this line:
Code java:
if(computer.move().compChoice.equals("(1,1)"))
The most important thing is that by saying computer.move(), you are calling the move() method again. I'm not sure if that is what you are intending. The second thing is that the move() method returns a String. However, that String just returns "compChoice" where I think you are attempting to return the value of compChoice. To return the value of compChoice, you need to adjust your return statement in your move() method to:
Also, you cannot access a Local Variable of a method without returning it (at least I don't think you can) which is what you are attempting with the statement:
Code java:
computer.move().compChoice
You can however access a Global Variable of an Object. This can be done two ways. The first is by saying:
This way will only work if the Global Variable is not declared private. The second way is by creating a method that returns that value. That method would look like this:
Code java:
public String getCompChoiceVariable()
{
return compChoice;
}
So, what needs to change?
1) You need to fix the return on your move() method.
2) Since you return a String with the move() method, why not capture it. To do so, change this statement:
to:
Code java:
String value = computer.move();
3) Now that we have captured that value, we can compare it in our IF Statement. So, instead of this:
Code java:
if(computer.move().compChoice.equals("(1,1)"))
we do this:
Code java:
if(value.equals("(1,1)"))
Does that all make sense?
Re: Getting a string value from within a method in another class
Yeah that makes sense. I used the first one so now the line of code looks like:
Code :
if(computer.compChoice.equals("(1,1)"))
But I have another question. Now im trying to check if the String space11 changes, but how can i if i initalize and assign it a value outside of the first method? So basically, how can I get the new value from the method space11?
This is the code for the space class.
Code :
public class space {
String space11 = "-";
String space12 = "-";
String space13 = "-";
String space21 = "-";
String space22 = "-";
String space23 = "-";
String space31 = "-";
String space32 = "-";
String space33 = "-";
public void space11 () {
compMove computer = new compMove();
computer.move();
if(computer.compChoice.equals("(1,1)")) {
space11 = "o";
}
}
public void printSpace11() {
System.out.println(space11);
}
}
Im trying to get the "o" appear from the printSpace111 method but it just uses the original variable.
Re: Getting a string value from within a method in another class
That could mean two things.
1) you are calling the printSpace11() method before you change its value.
2) your if statement is returning false
The second is probably more likely. What is the value of computer.compChoice during the if statement? To find out what the value is, put this code before the if statement:
Code java:
System.out.println(computer.compChoice);
Re: Getting a string value from within a method in another class
The if statement is supposed to return false sometimes because the computer randomly selects on of the values (1,1) or (1,2) or (1,3) and so on.
This is an example of me looping the run.
Code :
(2,3)
(2,3)
(1,2)
(1,2)
Press any key to continue . . .
(3,2)
(3,2)
(3,1)
(3,1)
Press any key to continue . . .
(1,1)
(1,1)
(1,1)
(1,1)
Press any key to continue . . .
It calls the method space11 2 times and compMove also prints out the compChoice string. Thats why theres 4 there.
I changed it to prove that the if statement returns true. I added a line that tells you if it did.
Code :
System.out.println("YES! SPACE11: " + space11);
It does work.
Re: Getting a string value from within a method in another class
Quote:
Originally Posted by
sp11k3t3ht3rd
Okay, I'm kind of new to Java but I still know some stuff. I'm trying to get a string value from a separate class that is within a method in that class. I can't figure it out! I'm making a tic-tac-toe game ( Text-Based ).This is the code for the computer moving class.
Code :
public class compMove {
String compChoice;
public String move() {
String[] rowsComp = {"(1,1)", "(1,2)", "(1,3)", "(2,1)", "(2,2)", "(2,3)", "(3,1)", "(3,2)", "(3,3)"};
int rowsCompLength = rowsComp.length;
int rand = (int) (Math.random() * rowsCompLength);
compChoice = rowsComp[rand];
System.out.println(compChoice);
return ("compChoice");
}
}
This is the code for the spaces that can be selected by either an X or an O.
Code :
public class space {
String space11 = "-";
String space12 = "-";
String space13 = "-";
String space21 = "-";
String space22 = "-";
String space23 = "-";
String space31 = "-";
String space32 = "-";
String space33 = "-";
public void space11 () {
compMove computer = new compMove();
computer.move();
if(computer.move().compChoice.equals("(1,1)")) {
space11 = "o";
}
}
public void printSpace11() {
space11 ();
}
}
As you can see I am trying to check if the value of the string compChoice which gets its value in the method move in the compMove class is equal to (1,1). This code does not compile but can someone tell me how to fix this line of code to work?
Code :
if(computer.move().compChoice.equals("(1,1)"))
Please Help!
It depends, if you defined that String inside that method, it's lost after the method ends. However, if the method returns that String, then you could call that String by
TheOtherClass toc = new TheOtherClass(parameters)
to get to the string in TheOtherClass, if it's returned by the method:
wait...if you're comparing a String in your current class to a String that is returned by the method in the other class, you do this:
Code java:
TheOtherClass toc = new TheOtherClass(parameters);
String str = "whatever";
String str2 = toc.theMethod(methodsParameters);
if (str.equals(str2))
{
// whatever
}
if you aren't returning it from that method, and you have that particular String as a public value in the constructor, then you can, maybe, get it by calling it like this:
Code java:
TheOtherClass toc = new TheOtherClass(parameters)
String str = "whatever";
String str2 = toc.otherStringVariableName;
// note, this will set it to null if it hasn't been set. It may only return what the constructor has set it to.
// I'm not certain on this one, but it can't hurt to try.
if (str.equals(str2))
{
// whatever
}
Unless you're comparing length or something else. Then do that comparison then if so.