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 2 of 2

Thread: how to pass a variable from a method to another

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to pass a variable from a method to another

    in this code, i would like to know how to pass the convertedWorkArray from the first method to the second one ??
    N.B:
    1-both methods belong to the same class
    2-i dont want the method ( getsum) to receive any parameters from the (main), i just want to know how to set the (convertedworkarray) visible to the method (getsum)

    public  int []convertStrToInt() 
        	{
        		int [] convertedWorkArray = new int [workarray.length];
     
        		for (int i=0;i<workarray.length;i++)
        			convertedWorkArray[i]=Integer.parseInt(workarray[i]);
        		    System.out.println("Your array was converted into integers");
                return convertedWorkArray;
            } 
     
     public int getSum()
         {
         	int sum=0;
         	for (int i=0 ; i< convertedWorkArray.length ; i++)
         		sum+ = convertedWorkArray[i];
         		return sum;               		
         }
    Last edited by amr; November 21st, 2010 at 07:14 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: how to pass a variable from a method to another

    You need to give the getSum() method a parameter that accepts an int[]. Then you need to send it the int[] when you call the method:
    public int getSum(int[] arr)
         {
            int sum=0;
            for (int i=0 ; i< arr.length ; i++)
                sum+ = arr[i];
                return sum;                    
         }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Can't pass to the other method
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 09:44 AM
  2. Replies: 0
    Last Post: April 17th, 2010, 08:29 AM
  3. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  4. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM
  5. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM