Results 1 to 6 of 6
  1. #1
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default [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 ( ) {
    
    ????
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:
    Java Code:
    public void myMethod(Scanner scan)
    {
      //... here use scan in method
    }
    and I call it like so:
    Java Code:
    public static void  main(String[] args)
    {
      Scanner scan = new Scanner(System.in);
      myMethod(scan);
      //..... more code
    }
    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 have
    Java Code:
    void myMethod(int[] intArray)
    {
      for(int i : intArray)
      {
        //.... do something here
      }
    }

  3. #3
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    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 ? )

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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.

  5. #5
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    ok.... i followed Supamagier instruction ... but there is still an error

    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 with int highest = myMethod(value); ,
    error message,non-static method myMethod(int[]) cannot be referenced from a static content.

  6. #6
    blueyan is offline Member
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Default

    haha , i just found out the solution , careless problem...forget to add "static" in second method.... thank to Supamagier and Fubarable who tried to help me ... problem solved , thread closed...

Similar Threads

  1. passing values from main page to pop up window
    By vicky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-29-2008, 03:06 PM
  2. Int Array Allowed in main() Method?
    By Starclopsofish in forum New To Java
    Replies: 19
    Last Post: 10-03-2008, 04:22 PM
  3. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 04:43 AM
  4. Replies: 0
    Last Post: 04-07-2008, 07:44 AM
  5. Document conversion PDF to MS doc and vice versa
    By abintoms in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:45 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
  •