• 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

Passing multiple variables between methods

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm making a program that will make an array of numbers, find the average of the array, then print all the numbers that are above the average. I'm having trouble passing the variable that I need between methods, so I could use some help. This is what I've got.



When I run compile that, it gives me this error:



Any help would be appreciated.
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable that is declared inside of a method is only visible inside that mathod.

Your int[] abc variable is declared inside your processData() method so it will not be visible inside the main() method yet you are trying to use it in the main() method.

You have a couple options here. Either declare static variables outside of the methods (at the class level), or you can return a value from the processData() method and then pass that back into the printData() method.

 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Winterbourne wrote:A variable that is declared inside of a method is only visible inside that mathod.

Your int[] abc variable is declared inside your processData() method so it will not be visible inside the main() method yet you are trying to use it in the main() method.

You have a couple options here. Either declare static variables outside of the methods (at the class level), or you can return a value from the processData() method and then pass that back into the printData() method.



How would I do that last option? Especially when I have to return two different types of variables?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can declare and initialize you int array inside the main method. that way you can pass it into both other methods and the processData() method would only have to return the average, not the array. This will leave the processData() method more general so it can be used to process any int array and be called by other methods.
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've ended up with this:



and got this error



Any ideas?
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
processData(avg,abc) needs a method with a signature of (int, int[]), but you don't have one.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zach rush wrote:
and got this error



Any ideas?



The error message is pretty clear. You are calling the processData() method, with two parameters -- the first is an int array, and the second is an int. That signature does not exist. The closest is a processData() method, with two parameters -- the first is an int and the second is an int array.

Henry
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I switched the variables in the main method, and now I get this error:

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zach rush wrote:Ok, I switched the variables in the main method, and now I get this error:



The error message is saying that at that line (line 17 of yup.java), there is code that cannot be reached. Looking at your program, can you figure out why?

hint: examine the lines of code *before* that line.

Henry
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

zach rush wrote:Ok, I switched the variables in the main method, and now I get this error:



The error message is saying that at that line (line 17 of yup.java), there is code that cannot be reached. Looking at your program, can you figure out why?

hint: examine the lines of code *before* that line.

Henry



Okay, I figured out that part, and the program works, but it seems to give me the wrong answer.

The code now looks like this:



And the output is this:



I looked over the code and I don't understand how it can say 1>5.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zach rush wrote:
I looked over the code and I don't understand how it can say 1>5.



It never said that 1>5. It said that 1 is greater than the average variable.... which if you print it out again, is not 5.

Henry
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't understand how the average does not equal 5? sum is equal to all the values in abc added together, then avg is equal to sum divided by the length of abc. where does the number get mixed up?

Edit: I found how avg = 0 when printData() is executed, but I can't figure out how to put "int avg = 0;" into the main method (since it will send me an error if I don't) without resetting it's value after processInfo() is executed.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your processData() method RETURN's the value of avg. You need to capture that returned value in the main() method and assign it to your avg variable that exists in the main method.

Right now the avg value is being passed back to the main() method as a return but not being assigned to the main() method's avg variable. You can assign the return value of a method call directly to a variable like you would with any other value.

With this in mind, look specifically at line 9 in your main() method. That method call returns a value. You need to assign that returned value to something in order to use it in your next printData() method.
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried doing this in the main method, but it says I need to initialize avg2, which will then reset the value if I do that. How do I make it so that a new variable is given avg's value without having to make a statement "avg2 = 0"?

 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zach rush wrote:I tried doing this in the main method, but it says I need to initialize avg2, which will then reset the value if I do that. How do I make it so that a new variable is given avg's value without having to make a statement "avg2 = 0"?



The variables declared inside a method are local to that method and the compiler expects us to initialize them or it would flag them as compiler errors.
In your case, avg2 is a variable local to the main method and hence the compiler expects you to initialize it.

To capture the value returned by a method in a variable, say var, I can simply code as follows.

var = method( <arguments of the method>);

For instance, if I need to capture the value returned by the method add in a variable sum, I can code it as follows.

 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have this, which it's giving me an error about the divide method:

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What divide method?

(Perhaps that's what the error message says... it's always helpful to TellTheDetails (<-- follow that link) when you ask questions on a forum.)
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is said error message:

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. So the compiler can't find a declaration for the symbol "divide". I don't see one either in your code. Did you expect "divide" to be a standard Java method or something like that?
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got confused by an answer that Heena gave me earlier.

I added the divide line back, but it gave me the same error. What am I doing wrong?



And this is the error I'm getting.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul already addressed that. There's no divide() method, so the compiler complains.
 
zach rush
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:



So how do I capture the value of the avg when it's transferred to the main method? I was using this as an example, and thought I could use divide instead of add, but I guess not.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The add() method in this example is just like your processData() method.



All you really need to pass into the processData() method is the array.



Note, both the avg and average variables are declared as doubles and the processData() method is has a return type of double. When doing division you may end up with a decimal number which will be truncated if cast to an int.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zach rush wrote:

Heena Agarwal wrote:



So how do I capture the value of the avg when it's transferred to the main method? I was using this as an example, and thought I could use divide instead of add, but I guess not.



No, I have not advised you to invoke a method that you have not declared or that your class has not inherited. You might want to read my response again.

Whenever you code as follows,



etc, meaning whenever a sequence of letters is followed by braces, i.e "(" and ")", a method is invoked.

Such a method should be present in your class or inherited by your class. So in your code, you have the following line.



what that means is this -
Call a method called divide with arguments sum and length.
Whatever value the method returns, assign it to avg.

So where is the divide method in your class? Have you coded any method with that name?

The compiler error is telling you that it can't find the symbol divide in your code.

Now coming to your method, processData, i.e


you are calculating average and returning the sum. is this method supposed to calculate the average or is it supposed to calculate the sum?
If average, why is the method returning the sum?

If I had to code this method, I would probably code it as follows. Oh and yes, I would change the method name from processData to computeAverage cause the name tells me what processing my method is doing.



Now when you call your processData method from the main method as follows.



the result of processData, i.e sum in your case ( though that is probably incorrect cause I think it should be average, not sum ) is passed to the caller.

But the caller needs to capture the result in a variable. Otherwise it is lost.

So if your caller method has an avg var such as..

and if you invoke a method that computes average, you might want to assign the result of the method computeAverage to the variable avg. This is how you do that.



So if you print avg after this, it will print the computed average.

But if you invoke the computeAverage as follows.


So if you print avg after this, it will print 0, the value you initialized the variable avg with.
 
zach rush
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whew! It works! I didn't realize that I needed to make avg equal the method processData, because I didn't realize that the value returned from that would equal avg. But I got it.

Thank you everybody that helped. I'm gonna be around here a little more than stack overflow, because they all yell at me when I don't know what I'm doing. Thank you again.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes indeed. Whenever you call a method that returns a value, you can just assign that returned value directly to a variable of the same type (or a super type or cast it, but that comes later) using the equals (=) operator.



just like when you have something like this:


The right side of the statement is evaluated and then assigned to the variable. the same thing happens with a method() call. The method is evaluated and the result (the returned value) is assigned to the variable.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heena Agarwal wrote:Whenever you code as follows,

view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
sum();
sum(a,b)
divide(),
divide(a,b);
anyMethod(b,c);


etc, meaning whenever a sequence of letters is followed by braces, i.e "(" and ")", a method is invoked.



Actually that is not entirely right.

Whenever or is coded, a constructor is called.

Of course when this sequence of letters is not preceded by the keyword 'new', a method is invoked.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic