aspose file tools*
The moose likes Beginning Java and the fly likes Unable to Return Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to Return Variable" Watch "Unable to Return Variable" New topic
Author

Unable to Return Variable

Jared Osborne
Greenhorn

Joined: May 12, 2009
Posts: 5
This program's supposed your typical random number generator. It doesn't seem to be passing the int Number back from the NumbertoGuess method for use in the rest of the program. What am I doing wrong here? I'm aware there are some missing things in this program, I'm concerned about getting the value returned properly right now.

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Nothing looks wrong with NumbertoGuess. It is returning the number.

The question is - What makes you think that its not returning a number?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Jared Osborne
Greenhorn

Joined: May 12, 2009
Posts: 5
Thanks for the help, Sam.

I've been doing some debugging with Ststem.out.println and trying to track the variables and their values as the program runs. If you execute the code, it will let you pick a number but will always tell you the number is lower than your guess. When you guess either a 0 or guess twice, it says you've guessed correctly and asks if you'd like to play again.

Far as I can tell, it's not passing the randomized Number variable back into the public void main for use in the next method in the series once it's generated in NumbertoGuess. I see the number generated correctly within NumbertoGuess but always see a value of 0 with a println at line 15 or 16 when I check it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Java is pass by value, so passing Number as a parameter to the method will not cause Number to be changed.
What you should have is


Note that I've changed the name of the variable in NumbertoGuess to make it obvious that it is not the same variable as the Number instance variable


Joanne
Jared Osborne
Greenhorn

Joined: May 12, 2009
Posts: 5
Alright, that makes sense. So how do I get the value of num out of the method for use in the rest of the program? Does the returning of num pass that value as the int num or back to NumbertoGuess?
Eric Mission
Greenhorn

Joined: Apr 22, 2009
Posts: 22
Your have several problems with this code...

1 You don't adhere to Sun's naming recommendations.

Sun Naming Conventions

2. Read this article to understand pass-by-value and pass-by-reference for Java.

Java parameter passing

The third issue is a result of trying to manipulate the parameter instead of capturing the return value from your method. NumbertoGuess returns a value- and you aren't storing it into anything!



to infinity and beyond
Eric Mission
Greenhorn

Joined: Apr 22, 2009
Posts: 22
Ooppss, sry Joanne! This rotten connection is so sloooow (didn't see your post) I might as well have mailed in my post-
Jared Osborne
Greenhorn

Joined: May 12, 2009
Posts: 5
That's what I get for going from a pass-by-reference language like VB to Java. lol

I'll mess around with it for a bit and see what I come up with. Thanks for the help!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Jared Osborne wrote:Alright, that makes sense. So how do I get the value of num out of the method for use in the rest of the program?


Thats what the line 'return num;' does.

Jared Osborne wrote:Does the returning of num pass that value as the int num or back to NumbertoGuess?


Note how I also changed your call to the method. The returned value will be assigned to Number
Jared Osborne
Greenhorn

Joined: May 12, 2009
Posts: 5
Workin' like a charm now. Thanks for the help, all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to Return Variable