Results 1 to 6 of 6
- 10-04-2008, 09:52 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
[SOLVED] passing array between main and method,vice-versa
my problem is , how to pass an array from main to method , then method send back to main again ?
here is my question, how do i pass this array of user input result to another method ? this method called max method , which determine the maximum value within the array...example , user input " 1 , 4 , 5 " , then array will be passed from main to max method , max method will check which integer is the highest then send back to main... i really do not have any idea about this , please help me.... thank and appreciate for the view and reply !!
Java Code:public static void main(String[] args) { int [] value = new int [3]; for (int i =0;i<value.length;i++){ String number_string =JOptionPane.showInputDialog("please enter an integer"); int number = Integer.parseInt(number_string); value[i]=number; } } public static int max ( ) { ???? }
-
It's a lot easier than you think. What do you do when you want to pass a Scanner object to another method? You use a parameter like so:
and I call it like so:Java Code:public void myMethod(Scanner scan) { //... here use scan in method }
So you do essentially the same for an array. What's its type? Well if an Array of ints, its type is int[], if Dates its Date[]. So in my method declaration section, I'd haveJava Code:public static void main(String[] args) { Scanner scan = new Scanner(System.in); myMethod(scan); //..... more code }
Java Code:void myMethod(int[] intArray) { for(int i : intArray) { //.... do something here } }
- 10-04-2008, 10:22 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
hmm ,,,i dont get it .... i just started to learn array yesterday....still not yet master , so i dont understand what do you mean =_= """ ...sorry...
so i specific my question.... how to declare max method in Main method which to be passed to max method ..( to pass user input results ? )
- 10-04-2008, 10:24 AM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Java Code:public static void main(String[] args) { int[] intArray = new int[10]; intArray = myMethod(intArray); } private int[] myMethod(int[] intArray) { // Do stuff return intArray; }I die a little on the inside...
Every time I get shot.
- 10-04-2008, 11:01 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
ok.... i followed Supamagier instruction ... but there is still an error
error with int highest = myMethod(value); ,Java Code:import javax.swing.JOptionPane; public class test { /** * @param args the command line arguments */ public static void main(String[] args) { int [] value = new int [5]; for (int i =0;i<value.length;i++){ String number_string =JOptionPane.showInputDialog("please enter integer"); int number = Integer.parseInt(number_string); value[i]=number; } int highest = myMethod(value); } public int myMethod(int[] value) { int max = value[0]; for (int i = 1; i < value.length; i++) { if (max < value[i]) max = value[i]; } return max; } }
error message,non-static method myMethod(int[]) cannot be referenced from a static content.
- 10-04-2008, 11:13 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
passing values from main page to pop up window
By vicky in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 12-29-2008, 03:06 PM -
Int Array Allowed in main() Method?
By Starclopsofish in forum New To JavaReplies: 19Last Post: 10-03-2008, 04:22 PM -
Converting to ASCII and vice-versa
By pheonix in forum New To JavaReplies: 2Last Post: 09-09-2008, 04:43 AM -
How do we exchange data between Java and JavaScript and vice-versa
By freddieMaize in forum Advanced JavaReplies: 0Last Post: 04-07-2008, 07:44 AM -
Document conversion PDF to MS doc and vice versa
By abintoms in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks