user1690599
user1690599

Reputation: 69

trouble defining array locally and passing parameters to other methods

My assignment was to write a Java class that creates an array of integers, fills it with values, prints the unsorted values, sorts the values into ascending order, and finally prints the sorted values.

For the most part I have done that and my output is fine. However I have not been able to define the array locally within the main(), and pass it as a parameter to the other methods. I try to define it as a static member of the class which cannot be done.

Can anyone help me out? I need to define the array in the main() and pass it as a parameter to the methods. But I just cannot figure it out despite tireless research.

Here is what I have so far.

public class ArraySort {
private static Object sc;
       int[] array;

// creates question and int for user input
/**
 *
 */
public void fillArray() {
    Scanner keyboardScanner = new Scanner(System.in);
    System.out.println("Enter the size of the array (3 to 10): ");
    int n = keyboardScanner.nextInt();
    array = new int[n];

    // creates new question by including int 
    System.out.println("Enter " + n + " values" );

    // creates for loop for repeating question based on array size 
    for (int i=0; i<n; i++) {
        System.out.println("Enter value for element " + i + ": ");
    array[i] = keyboardScanner.nextInt();
    }
}

// prints i in the for loop
public void printArray(String msg) {
    System.out.println(msg);
    for (int i=0; i<array.length; i++) {
        System.out.println(array[i]);
    }
}

//  defines method
public void sortArray() {
    // sets up to output in ascending order
    for (int i=0; i<array.length; i++) {
        for (int j=i+1; j<array.length; j++) {
            if (array[i] > array[j]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}



// main output and visual layout
public static void main(String[] args) {
    ArraySort arraySort = new ArraySort();
    arraySort.fillArray();


    System.out.println();
    arraySort.printArray("The unsorted values... ");
    arraySort.sortArray();
    System.out.println();
    arraySort.printArray("The sorted values... ");




    // Keep console window alive until 'enter' pressed (if needed).
    System.out.println();
    System.out.println("Done - press enter key to end program");



 }
}

I have no errors, I just need help on how to define the array locally in the main()

Thanks.

Upvotes: 1

Views: 2306

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213271

You can declare your array locally in your main method. And pass it as a parameter to the method you are calling.

Since when you pass array as parameter to another method, its reference will be copied to the parameter. So any change you make to passed array in your method, will get reflected back in your main() method in your original array. Try using this. I don't think you will face any problem.

UPDATE: - Ok, here's the modified code: -

import java.util.Scanner;

public class ArraySort {
private static Object sc;    
private static Scanner keyboardScanner = new Scanner(System.in);

// creates question and int for user input
/**
 *
 */
public void fillArray(int[] array) {

    // creates for loop for repeating question based on array size 
    for (int i=0; i<array.length; i++) {
        System.out.println("Enter value for element " + i + ": ");
        array[i] = keyboardScanner.nextInt();
    }
}

// prints i in the for loop
public void printArray(String msg, int[] argsArray) {
    System.out.println(msg);
    for (int i=0; i<argsArray.length; i++) {
        System.out.println(argsArray[i]);
    }
}

//  defines method
public void sortArray(int[] array) {
    // sets up to output in ascending order
    for (int i=0; i<array.length; i++) {
        for (int j=i+1; j<array.length; j++) {
            if (array[i] > array[j]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

}

// main output and visual layout
public static void main(String[] args) {

    System.out.println("Enter the size of the array (3 to 10): ");
    int n = keyboardScanner.nextInt();

    int[] array = new int[n];

    ArraySort arraySort = new ArraySort();
    arraySort.fillArray(array);

    System.out.println();
    //I still get an error saying " cannot find symbol"
    arraySort.printArray("The unsorted values... ", array);
    //same here
    arraySort.sortArray(array);

    System.out.println();
    //and here
    arraySort.printArray("The sorted values... ", array);


    // Keep console window alive until 'enter' pressed (if needed).
    System.out.println();
    System.out.println("Done - press enter key to end program");

}
}

Upvotes: 0

pb2q
pb2q

Reputation: 59617

You've said that you need to define the array in the main() and pass it as a parameter to the methods. If so, then you'll need to change those methods, e.g.:

public void printArray(String msg, int[] argsArray)

public void sortArray(int[] array)

fillArray is different, because you create the array in the method based on a size which is input by the user, so you can't simply pass the array as an argument. You can return the new array:

public int[] fillArray()
{
    int[] array;
    // ...

    return array;
}

But if you're only trying to test your class then note that you have access to the ArraySort.array field from main: you can set the field to an array that you create in main.

So in main:

ArraySort arraySort = new ArraySort();
arraySort.array = new int[]{ 5, 4, 3, 6, 7};

// and remove the fillArray call 
// ...

Note that if you want to set the array like this, then you should also remove the fillArray call, which tries to set the array from user-entered values.

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

Update public void printArray(String msg) { and public void sortArray() { to accept int [] as

  / prints i in the for loop
 public void printArray(String msg, int[] argsArray) {
    System.out.println(msg);
    for (int i=0; i<argsArray.length; i++) {
        System.out.println(argsArray[i]);
    }
 }

 //  defines method
 public void sortArray(int[] argsArray) {
   // sets up to output in ascending order
   for (int i=0; i<argsArray.length; i++) {
    for (int j=i+1; j<argsArray.length; j++) {
        if (argsArray[i] > argsArray[j]) {
            int temp = argsArray[i];
            argsArray[i] = argsArray[j];
            argsArray[j] = temp;
        }
      }
   }
 }

And you may want to leave array = new int[n]; in fillArray as local as:

    int[] array = new int[n];

and remove it from class variable declaration.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691775

Remove the int[] array; declaration from the class. Add it to the main method:

int[] array = new int[n];

And add an argument int[] array to each method which needs to access it. For example:

public void printArray(String msg, int[] array) {
    ...
}

Upvotes: 1

Related Questions