Two Laptop Bag*
The moose likes Java in General and the fly likes Passing multiple variables between methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing multiple variables between methods" Watch "Passing multiple variables between methods" New topic
Author

Passing multiple variables between methods

zach rush
Greenhorn

Joined: Feb 06, 2014
Posts: 12
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.
Scott Winterbourne
Ranch Hand

Joined: Jan 30, 2014
Posts: 114
    
    1

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

Joined: Feb 06, 2014
Posts: 12
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

Joined: Jan 30, 2014
Posts: 114
    
    1

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

Joined: Feb 06, 2014
Posts: 12
I've ended up with this:



and got this error



Any ideas?
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 292
    
    5

processData(avg,abc) needs a method with a signature of (int, int[]), but you don't have one.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
zach rush
Greenhorn

Joined: Feb 06, 2014
Posts: 12
Ok, I switched the variables in the main method, and now I get this error:

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Joined: Feb 06, 2014
Posts: 12
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
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Joined: Feb 06, 2014
Posts: 12
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

Joined: Jan 30, 2014
Posts: 114
    
    1

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

Joined: Feb 06, 2014
Posts: 12
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"?

Heena Agarwal
Ranch Hand

Joined: Dec 25, 2013
Posts: 261
    
    4
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

Joined: Feb 06, 2014
Posts: 12
Now I have this, which it's giving me an error about the divide method:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Feb 06, 2014
Posts: 12
This is said error message:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Feb 06, 2014
Posts: 12
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.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Paul already addressed that. There's no divide() method, so the compiler complains.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
zach rush
Greenhorn

Joined: Feb 06, 2014
Posts: 12
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

Joined: Jan 30, 2014
Posts: 114
    
    1

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

Joined: Dec 25, 2013
Posts: 261
    
    4
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

Joined: Feb 06, 2014
Posts: 12
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

Joined: Jan 30, 2014
Posts: 114
    
    1

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.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
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.
 
Consider Paul's rocket mass heater.
 
subject: Passing multiple variables between methods
 
Similar Threads
Input continuously collecting
Counting in Array List/ Standard deviation
Scanner class help
While Loop Problem(s)
Reading from a file