• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning Values

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, hoping I can get some help with the following program. I'm trying to return the Factorial calculation in my "public int fact" method to the first method where the user inputs their number. I'm not sure if I'm on the right track, I think I either have a problem with variable scope or the access modifiers (or both!) I have been messing around with code all week trying to figure it out but it's just not clicking for me. You'll notice that I have commented out some of my code at the System.out.println portion, I have been trying to return the factorial calculation to print to the screen. I have a few other components to add to the code but I would like to get what I have written so far to work if possible. Sorry if I'm doing a terrible job of explaining the issue, hopefully I'm making some sense! Thanks in advance.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You are very close! And you are right the problem is a variable scope.
The variable factorial is declared inside a method and therefore is local to this method. It does not exist outside of this method.

You should not try to access a local variable that is used in a return statement.

You should actually call the method using its name fact with a value a.

You are already calling a method (but without parameters) on line 22.
So you should know how to do this.

In case of problems don't be afraid to ask.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods are like a black box where you put something inside it, it processes it and gives out some output. They take more than one parameters but then can output at most 1 value.
Of course they can modify field variables, but lets not go there for now.

If I read your program correctly, You are creating an object of Assign1 in your main method.
In the constructor, you are calling input() method, it does not take any parameters, nor does it return any value.
But now, you need to call your fact() method. And, it isn't a simple method, it takes a parameter as input, so, you need to pass something to it...
eg. fact(10) will calculate the factorial of 10.

Next, it gives an output too. So, you need to "capture" its output to another variable.
eg. int calculatedValue = fact(10);

Hope this helps

As a side note, I suggest you give it a meaningful name, probably a verb.
so instead of input, you can call it, getUserInput(), instead of fact, you can call it calculateFactorial, etc.
 
mark bradshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply! Yes, around lines 22 - 24 I have tried a few different things. I added fact(a); but that isn't doing anything. I just can't get my head around where I should put it (what line) or if I need to put it between the Assign1 brackets. It's good to know that I'm close to getting it right.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to assign the returned value to a variable if you want to use it.*

Like:
int someVariable = someMethod(someParameter);

*Well, you could just put a call to a method inside other method's parameters list, like method1(method2(7)), but don't bother with this right now.
 
mark bradshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help, I finally figured it out. I have now added my if / else statement. Now, you'll notice that my while loop is dependent on "int userNumber = -100" I'm wondering if there is a way around this with a few subtle changes or would I need to start from scratch?

 
reply
    Bookmark Topic Watch Topic
  • New Topic