• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Passing value of one method to another

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, How can I pass the answer from method to another method to be calculated again?



Thanks as always.
 
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 31 and 36 are not doing what you might think they're doing.

What was your intent in assigning a value to myResult in the return statement? What do you think that does?

In reality, it does nothing useful.  In fact, the reference to myResult on line 36 looks like it might even cause a compile-time error.
 
Kevin O'Sullivan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Lines 31 and 36 are not doing what you might think they're doing.

What was your intent in assigning a value to myResult in the return statement? What do you think that does?

In reality, it does nothing useful.  In fact, the reference to myResult on line 36 looks like it might even cause a compile-time error.



My thinking was it would store the value and then I could use it again.
 
Saloon Keeper
Posts: 8460
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is incorrect. The parameter "myResult" will be filled locally on line 3 but that value will never make it back to the caller via "myResult", only by the return of the method. So, just remove "myResult" from this method and you'd be good to go.
This is still uses "myResult". Instead, just send back the results of num1*num2;
Like this...
While it's not a hard and fast rule, Java programmers have come to think of methods that begin with 'get....' as a "getter" used to access the fields of your class. Your method "getResult" would be easier to read if it was renamed "calcResult".
 
Junilu Lacar
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the calculation performed by the method is a * b, then the best name for that method really would be product(a, b) or productOf(a, b), in my opinion.

Also, the two methods seem to have the same intent: to multiply two numbers and return the product. You only need one method then.
 
Ranch Hand
Posts: 235
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understood this correctly, you are asking the user to input two (2) integers, multiply these two numbers together, and store the result.  Rinse and repeat.



Check the syntax on the println method.

Of course, We Are Not a Codemill, but this should get you on the right path, and hopefully a clear  this up for you,  Sorry about the crappy formatting -- something to do with the way I did the copy, cut, and paste.

Regards,
Robert
 
Kevin O'Sullivan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help so far, I'm very grateful,was never expecting the answer to be given to me out right, as that wouldn't be good as I wouldn't learn,I just wanted to be put on the right track, I think its possible ye misunderstood what I'm asking or I didn't explain it very well, so I will try to explain it better, if you have one method that does a calculation and you want the answer of that calculation to be passed to another method to be used in another calculation, how would I go about doing this? even if you could suggest reading material, I would be grateful.
 
Junilu Lacar
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin, things you need to do:

1. Save the value in a variable.
2. Pass values as parameters to methods.

That's it. These are the Lego blocks. There's no set recipe that we can point you to. It all depends on exactly what you want to do. It's up to you to find the ways to put these together to do what you want to do.  Another analogy is that these are the notes, each note will play a tone. It's up to you to arrange the notes in a way that forms a tune and create some music of your own.
 
Carey Brown
Saloon Keeper
Posts: 8460
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the values returned from a method directly. They don't always need to be put into a variable first.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic