Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

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

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default 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.

    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.

    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?

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

    Please Help!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    Return the variable from the move method
     
    public String move() {
        ....
     
        return compChoice;
    }
     
    ....
    String compChoice = computer.move();
    if ( compChoice.equals("1,1") ){
     
    }

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    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:
    computer.move();
    to:
    String value = computer.move();
    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/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    sp11k3t3ht3rd (October 9th, 2010)

  5. #4
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default 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?

    This is the code for the space class.

    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.
    Last edited by sp11k3t3ht3rd; October 9th, 2010 at 12:05 PM.

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    System.out.println(computer.compChoice);
    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/

  7. #6
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default 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.

    (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.
    System.out.println("YES! SPACE11:  " + space11);

    It does work.
    Last edited by sp11k3t3ht3rd; October 9th, 2010 at 01:00 PM.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

    Quote Originally Posted by sp11k3t3ht3rd View Post
    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.

    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.

    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?

    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:

    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:

    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.

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    sp11k3t3ht3rd (October 11th, 2010)

Similar Threads

  1. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  2. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  3. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  4. string method
    By dstha in forum Object Oriented Programming
    Replies: 3
    Last Post: February 25th, 2010, 08:07 PM
  5. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM