4

I have an assignment in which I have to perform operations on array in Java, I have to make separate functions of each operation, which I will write but I can not figure out how to invoke a method with array parametres. I usually program in c++ but this assignment is in java. If any of you can help me, I'd be really thankful. :)

public class HelloJava {
    static void inpoot() {
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
                System.out.println(numbers[i]); 
        }
    }

    public static void main(String[] args) {
        inpoot();
        outpoot(numbers); //can not find the symbol
    }
}
t0mppa
  • 3,983
  • 5
  • 37
  • 48
user3054791
  • 75
  • 1
  • 1
  • 7

2 Answers2

10

Your inpoot method has to return the int[] array, and then you pass it to outpoot as a parameter:

public class HelloJava {    
    static int[] inpoot() { // this method has to return int[]
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
        return numbers; // return array here
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
            System.out.println(numbers[i]); 
        }
    }

     public static void main(String[] args) {
        int[] numbers = inpoot(); // get the returned array
        outpoot(numbers); // and pass it to outpoot
    }
}
Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
-1

When you call outpoot it should be outpoot (numbers);

Anthony Porter
  • 152
  • 1
  • 9
  • it doesn't work, I've tried. The "can not find the symbol" error appears. – user3054791 Dec 01 '13 at 16:25
  • It's because numbers is a local variable declared in the inpoot method. It's out of scope I'm the main method – Anthony Porter Dec 01 '13 at 16:27
  • And that's what I'm asking, how do I pass the numbers array there. I mean, what do I do to make it take the array from inpoot and pass it in the outpoot call in main function. – user3054791 Dec 01 '13 at 16:30
  • You could make the inpoot method return the array instead of a void. And then in the main method pass it in as a param. – Anthony Porter Dec 01 '13 at 16:38