The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

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.

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?

Re: Getting a string value from within a method in another class

Ok, there are a few things to note about this line:

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:

return compChoice;

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:

computer.move().compChoice

You can however access a Global Variable of an Object. This can be done two ways. The first is by saying:

computer.compChoice;

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:

3) Now that we have captured that value, we can compare it in our IF Statement. So, instead of this:

if(computer.move().compChoice.equals("(1,1)"))

we do this:

if(value.equals("(1,1)"))

Does that all make sense?

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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:

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?

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: Getting a string value from within a method in another class

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.

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?

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:

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:

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.