Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:50:28+00:00 2026-06-12T23:50:28+00:00

My assignment was to write a Java class that creates an array of integers,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:50:29+00:00Added an answer on June 12, 2026 at 11:50 pm

    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");
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm taking a beginning Java class and an assignment requires that I write a
for an assignment in my Java class I'm supposed to write a program for
Here is the assignment verbatim: 1.) Write a Java program that is a TCP
ok so my assignment I'm supposed to write a class that stores a temperature
My Java assignment is to to write a Java application that uses data from
My class assignment is to write a program that has the user input a
*My assignment is to write a program that stores an array of five int
My kiddo had a homework assignment to write Blackjack in Java. I helped him
My assignment is to write an overloaded version of iquote() , a method that
I have an assignment that wants me to write an ternary search algorithm and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.