Results 1 to 6 of 6
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default how to pass parameters from a method to another which accepts to parameters?possible?

    in this code, i would like to know how to pass the convertedWorkArray from the first method to the second one ??
    N.B:
    both methods belong to the same class
    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)

    Java Code:
     public  int []convertStrToInt() 
        	{
        		int [] [COLOR="Red"]convertedWorkArray[/COLOR] = 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<[COLOR="red"]convertedWorkArray[/COLOR].length ; i++)
         		sum+ = convertedWorkArray[i];
         		return sum;               		
         }
    
    	public static void main (String [] args)
          {
              Op obj = new Op();
              Scanner read = new Scanner (System.in);      
              String scanchoice;
              int size, choice,value;
              
              do
              {
      	          System.out.println("choose an operation: 1:+ 2:- 3:* 4:/");
                  scanchoice = read.nextLine();
                  choice = Integer.parseInt(scanchoice);
                  
    	          switch (choice)
    	             {
    		           case 1 : System.out.println("enter number of elements:");
    		                      value=read.nextInt();
    		                      obj.setsize(value);
    		                      obj.initialize();
    		                      obj.getnumofoperands();
    		                      obj.[COLOR="Red"]convertStrToInt[/COLOR]();
    		                      System.out.println("Thu Sum = "+obj.[COLOR="Red"]doSum());[/COLOR]
    		                     /* pluseop(operand1,operand2);
    		           case '2' : initialize();           
    			                  subop(operand1,operand2);
                       case '3' : initialize();
            	                  mulop(operand1,operand2);
                       case '4' : initialize();
    	                          divop(operand1,operand2);*/
    	             }// end switch
    	      } while(choice!=1 || choice!=2 || choice!=3 || choice!=4); // end do-while
    	      
           }  // end main    
    } //end class
    Last edited by amrmb09; 11-21-2010 at 01:30 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    If you don't want to pass that array around as a parameter you could define it to be a member variable, so take out the definition from the method and stick it back in at the class level.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    Mr. Jos,
    do ou mean the following:
    to define the convertedWorkArray outside the methods so that to be accessible by all member methods??

    Java Code:
    public class Op
    {
    	public  int arraysize;
    	private double operand1, operand2;
    	private int numofoperands;
    	
        Scanner read=new Scanner(System.in);
    	String scandata, scaninput;
    	int scannumofoperands,scanoperandsindex;
    	public  String [] workarray;
    	public String [] dataarray;
    [COLOR="Red"]	public int [] convertedWorkArray;[/COLOR]
    
    method1
    {
    ...
    ...
    }
    
    method2
    {
    ...
    ...
    }
    
     public  int []convertStrToInt() 
        	{
        		 [COLOR="red"]convertedWorkArray [/COLOR]= 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<[COLOR="red"]convertedWorkArray[/COLOR].length ; i++)
         		sum+ = convertedWorkArray[i];
         		return sum;               		
         }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by amrmb09 View Post
    Mr. Jos,
    do ou mean the following:
    to define the convertedWorkArray outside the methods so that to be accessible by all member methods??
    Yep, that's what I mean but I'd personally go with the parameter passing way ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    is it what you meant by parameter passing to the the methods??
    N.B: in the (main) i dont want the method (getSum) to accept any parameters,

    Java Code:
    int [] convertStrToInt ()
    {
      String [] 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
    }
    
    int getSum(String [] convertedworkarray)
    {
        int sum=0;
         	for (int i=0 ; i<convertedWorkArray.length ; i++)
         		sum+ = convertedWorkArray[i];
         		return sum; 
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by amrmb09 View Post
    is it what you meant by parameter passing to the the methods??
    N.B: in the (main) i dont want the method (getSum) to accept any parameters,
    So, do as I suggested and make that array a member variable; it'll be visible to each method and doesn't need to be passed in as a parameter.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 2
    Last Post: 10-24-2010, 04:34 PM
  2. how to use picturebox and pass parameters to a url
    By Nubkadiya in forum New To Java
    Replies: 5
    Last Post: 06-02-2010, 02:00 PM
  3. Need to dynamicaly get all parameters in a method call.
    By DavidJohns in forum Advanced Java
    Replies: 2
    Last Post: 05-21-2008, 07:15 AM
  4. Replies: 9
    Last Post: 12-18-2007, 10:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •