• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Unable to Return Variable

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing looks wrong with NumbertoGuess. It is returning the number.

The question is - What makes you think that its not returning a number?
 
Jared Osborne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jared Osborne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Eric Mission
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Workin' like a charm now. Thanks for the help, all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic