Results 1 to 7 of 7
  1. #1
    WoodyHD is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Read in Int from return method in other class?

    I have this method in a class called Player. Basically what's going on is this method in the Player class is preforming a check and returning an integer value based on that.


    public int checkName(String name) throws IOException{


    I want to be able to use that integer value back in my main, but can't figure out how to make it work. (come to think of it there's a lot of things I can't figure out how to make work )
    I know it's probably simple, but I'm still learning and it's been a slow process.

    Back in my main I have


    Player ans = new Player();
    ans.checkTaken(name);


    error: The method checkTaken(String) is undefined for the type Player
    Last edited by WoodyHD; 01-18-2012 at 03:54 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Read in Int from return method in other class?

    error: The method checkTaken(String) is undefined for the type Player
    That's because the method is checkName().

  3. #3
    Jason1 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Read in Int from return method in other class?

    HI WoodyHD,

    I'm not sure where your trying to read the names from, but you can certainly use something like this.

    If you have questions about whats going on in this code please let me know.

    Java Code:
    public class Main {
    	public static void main(String args[]) {
    		int x = 0;		
    		Player player = new Player();
    		try {
    			x = player.checkName("Jason");
    		} catch (IOException e) {
    			e.printStackTrace(); 
    		}
    		System.out.println(x);
    	}
    }
    public class Player { 	
    	String name = "john";
    	public int checkName(String name) throws IOException{
    		if (this.name.equals(name)) {
    			return 1;	
    		}
    		else{
    			return 0;
    		}
    	}
    }
    Hope this helped and good luck!
    Last edited by pbrockway2; 01-18-2012 at 04:37 AM. Reason: code tags added

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Read in Int from return method in other class?

    Hi Jason1, welcome to the forums.

    When you post code you can ensure that it's readable by using code tags: put [code] at the start of the code and [/code] at the end.

  5. #5
    WoodyHD is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Read in Int from return method in other class?

    Quote Originally Posted by pbrockway2 View Post
    That's because the method is checkName().
    Well that's a face palm. You know I spent a good hour and half moving crap around and didn't notice that. I still have a related problem keeping me from using that int back in the main.

    if (ans == 1){

    error: Incompatible operand types Player and int

    I appreciate the help.


    Edit: Never mind. Swear I didn't do a thing, but the error disappeared. Now I got new problems to deal with :)
    Last edited by WoodyHD; 01-18-2012 at 04:48 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Read in Int from return method in other class?

    You're welcome.

    It's all just a matter of getting used to the compiler messages. (I don't know how Java is taught, but my belief is that some time should be spent on this right at the start. To take the sting out of compiler messages and also to give the novice programmer some techniques for addressing the concerns they raise.) Most of these messages are straight forward, but they use a stylised language that is not clear until it's explained.

    "The method checkTaken(String) is undefined for the type Player"

    This one means one of two things: (A) you sent it the wrong type eg you sent a method a String but it wanted an int or something, or (B) you made a typo in the method name. There are no other possibilities, and if you know that you won't spend time chasing more elaborate "solutions".

  7. #7
    Jason1 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Read in Int from return method in other class?

    Thanks pbrockway2,

    Had no idea you could do that...

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  2. Why is this method return 0
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-30-2011, 06:21 PM
  3. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  4. Replies: 4
    Last Post: 11-25-2008, 05:53 AM
  5. how to read single Keystroke without using return
    By oomanamemni in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •