0

I'm stuck on a Java coding assignment. I'm supposed to create three methods (askInfo, copyInfo, and setArray) for an existing program that lets the user input numbers into an array, and then organizes and prints that array. AskInfo receives the array as a parameter, asks for the index values from the user, and returns the amount of inputted numbers as int. CopyInfo copies the values into a new array with a length of the returned amount of numbers, and setArray sorts the numbers.

My problem is that, according to the assignment, askInfo is only supposed to return the amount of inputted numbers. Thus it leaves the values of the array printed with it inside the method, making copyInfo unable to retrieve those values and copy them into the second array. How would you suggest I get past this problem without the ability to edit the main method?

Main method:

    int[] tempArray = new int[100];
    System.out.println("Type in numbers. Type zero to quit.");
    int amountOfNumbers = askInfo(tempArray);

    int[] realArray = new int[amountOfNumbers];
    copyInfo(realArray, tempArray);

    setArray(realArray);

    printArray(realArray);

My code:

public static int askInfo (int[] tempArray) { //asks for numbers and assigns them to tempArray, returns the length of realArray
    int count = 0;
    Scanner reader = new Scanner(System.in);
    for (int i =0;i<tempArray.length;i++){
        System.out.print((i+1)+". number: ");
        tempArray[i] = reader.nextInt();
        count++;
        if (tempArray[i] == 0) //stops asking for values if user inputs 0
            return count;
        }
    return count;
    }

public static int[] copyInfo (int[] realArray, int[] tempArray)  { //copies tempArray's values to realArray
    for (int i=0; i<realArray.length;i++){
        realArray[i] = tempArray[i];
    }
    return realArray;
}

public static int[] setArray (int[] realArray) { //sorts realArray from largest value to smallest
    for (int i=0;i<realArray.length;i++){
        for (int j=i+1;j<realArray.length;j++){
            if (realArray[i]<realArray[j]){
                int tmp = realArray[i];
                realArray[i] = realArray[j];
                realArray[j] = tmp;
            }
        }
    }
    return realArray;   
}

Right now the program does compile, but the values of the arrays realArray and tempArray get outside of the askInfo method are null. Keep in mind that I cannot edit the main method - I can only edit the three methods I wrote.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • Do you have the ability to edit the class that these methods are a part of? If so, you can create a class variable and edit THAT. – Nick Vitha Apr 08 '19 at 20:05
  • 1st, you can use ```Arrays.sort(realArray)``` to sort the array. This will eliminate the method setArray(). 2nd, you can use the method clone() as ```realArray = tempArray.clone()```, ```realArray = Arrays.copyOf(tempArray, tempArray.length)```, ```Array.Stream(...)```, or ```System.arraycopy(...)``` – acarlstein Apr 08 '19 at 20:08
  • This requirement is similar to the Context design pattern: https://stackoverflow.com/questions/771983/what-is-context-object-design-pattern Instead of passing around the array for permutation, you pass around an Object containing the array for permutation, allowing you to keep the object in scope as needed and with permutations without explicitly requiring it to be returned. – Compass Apr 08 '19 at 20:11
  • Welcome to Stack Overflow. I suggest you read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips on debugging your code. If you still need help, please [edit] your question so that the code is complete. We can help you better if you provide a complete code example that we can copy/paste and run ourselves. – Code-Apprentice Apr 08 '19 at 20:32

3 Answers3

1

The code you wrote, per se, is correct. You don't need any kind of modification.
You say

the values of the arrays realArray and tempArray get outside of the askInfo method are null

That statement is not true at all.
The int[] tempArray array is effectively being changed inside askInfo.
And int[] realArray is effectively being populated.

enter image description here

I suggest you to take a break and come back to this exercise with a fresh mind.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
1

Is it just me or are you missing your printArray method? Other than that the code looks good.an array is a reference type so all methods have access to the same TempArray even though the values in it were added inside a method

user9791370
  • 339
  • 1
  • 10
  • Thank you for your contributions to Stack Overflow. This area is for answers only. Keep working on your reputation so that you can post comments to others' questions for clarification such as this. – Code-Apprentice Apr 08 '19 at 20:40
  • 1
    Sorry for the confusion, I meant it as an answer not a comment- sorry if it comes off as sounding like one – user9791370 Apr 08 '19 at 20:43
  • How does this answer the question in the title? Can you add more details? – Code-Apprentice Apr 08 '19 at 20:44
-1

In Java, array variables are references to array objects. This means that when you assign tempArray[i] = reader.nextInt(); in askInfo(), it modifies the content of the array named tempArray in main(). For more info read Are arrays passed by value or passed by reference in Java?.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268