Results 1 to 6 of 6
- 11-21-2010, 12:12 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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 classLast edited by amrmb09; 11-21-2010 at 12:30 PM.
- 11-21-2010, 12:35 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2010, 01:27 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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; }
- 11-21-2010, 01:35 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 11-21-2010, 01:54 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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; }
- 11-21-2010, 02:08 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Count the numbers of parameters programmatically that a method has in JAVA?
By rsegecin in forum Advanced JavaReplies: 2Last Post: 10-24-2010, 04:34 PM -
how to use picturebox and pass parameters to a url
By Nubkadiya in forum New To JavaReplies: 5Last Post: 06-02-2010, 02:00 PM -
Need to dynamicaly get all parameters in a method call.
By DavidJohns in forum Advanced JavaReplies: 2Last Post: 05-21-2008, 07:15 AM -
Netbeans 6.0 - code completion of method parameters
By mwildam in forum NetBeansReplies: 9Last Post: 12-18-2007, 09:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks