Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Passing variables with void methods

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by knightmetal; March 21st, 2012 at 08:44 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Initializing and Receiving Variables, Creating Methods
    By RadiantChaos in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 12:01 PM
  2. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 10:10 PM
  3. How to combine these 3 void methods?..
    By TimoElPrimo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 8th, 2011, 02:25 PM
  4. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM