Passing variables with void methods
Hello,
Not sure if this is either too easy or just impossible. Here's the story: I'm processing data from a sensor and I have several methods that do a specific task with those input values.
The actual code is a bit long so there's no need to have you read it all. I wrote something simpler with the basic idea of what I'm trying to do. I'll try to explain it first:
Let's say I have 5 void methods: A, B, C, D and main ("main" is not really the problem here though).
1. Method "A" passes a variable (int data) to method "B"
2. Method "B" does something with that variable.
3. Method "A" also passes the variable "data" to method "C".
4. Now, method "B" and method "C" want to pass the processed data (two variables = one variable from each method) to one more method called "D", which will give me a final result.
So basically, method "D" is taking a variable from both method "B" and "C". I've been trying to do that but I get an error in method "B" and "C" saying that method "D" requires two variables, not one. It's obvious, I'm giving it only one variable per method but method "D" needs two arguments. I hope you're still reading this and sorry if I made it complicated, please check the code below, very simple:
Code :
package passing;
public class Passing {
Passing p;
public static void main(String[] args) {
new Passing().sum();
}
public void sum() {
int x = 3;
int y = 7;
int mySum1 = x + y;
int mySum2 = 2 * x + y;
mult(mySum1, mySum2);
add5(mySum1, mySum2);
}
public void mult(int pass1, int pass2) {
int myMult1 = pass1 * pass2;
int myMult2 = myMult1 * myMult1;
total(myMult1, myMult2);
}
public void add5(int a, int b) {
int add = a + b + 5;
total(add);
}
public void total(int r1, int r2, int r3) {
System.out.println(r1+ "\t"+ r2);
}
}
Any help on this will be much appreciated, thanks.
Re: Passing variables with void methods
You've specified your total method as requiring THREE input parameters, so you must give it THREE values.
Giving £1 to a store which you owe £3 to isn't going to cut it, you must give it what is needed.
Re: Passing variables with void methods
Hello newbie,
Well that's the thing. I want to pass 2 variables from the multi() method and 1 variable from add5() method. 1 + 2 = 3 :)
Re: Passing variables with void methods
Can you post the full text of the error messages and the code that give the errors?
The arguments that you pass to a method must be defined and have their values set when you call the method.
One method can NOT access variables that are local to another method.
Re: Passing variables with void methods
Thanks and sorry, I'll try to make it clearer:
Please look at the mult() and add5() methods:
The question is, how do I pass "myMult1", "myMult2" and "add" variables to the "total()" method?
I made a mistake in the total() method, I actually wanted to print r1, r2 and r3 but I printed only two :/ something like this:
System.out.println(r1+ "\t"+ r2 + "\t" + r3);
What I basically need is, I want my total() method to receive 3 arguments, 2 arguments are coming from the "mult()" method and the other one from the "add5()" method. Sorry about the names I called my methods, I should've just called them A, B, C...etc to make it easier.
Re: Passing variables with void methods
For a method to pass variables as arguments to another method, all the variables must be known and accessible to the code inside of the calling method.
You will have to move some of the variable definitions to the class level so that they are available to the method that is doing the calling.
One method can NOT access variables that are local to another method.