Results 1 to 7 of 7
- 01-18-2012, 03:48 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
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 PlayerLast edited by WoodyHD; 01-18-2012 at 03:54 AM.
- 01-18-2012, 04:09 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Read in Int from return method in other class?
That's because the method is checkName().error: The method checkTaken(String) is undefined for the type Player
- 01-18-2012, 04:25 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
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.
Hope this helped and good luck!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; } } }Last edited by pbrockway2; 01-18-2012 at 04:37 AM. Reason: code tags added
- 01-18-2012, 04:39 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 01-18-2012, 04:42 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
Re: Read in Int from return method in other class?
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.
- 01-18-2012, 05:15 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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".
- 01-18-2012, 06:02 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Why is this method return 0
By africanhacker in forum New To JavaReplies: 7Last Post: 06-30-2011, 06:21 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
Help: how can get method, arg, return type... from a file .class
By ykzforever in forum New To JavaReplies: 4Last Post: 11-25-2008, 05:53 AM -
how to read single Keystroke without using return
By oomanamemni in forum New To JavaReplies: 1Last Post: 02-14-2008, 07:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks