Question:
How to return an array to main method?
gkeown
2008-12-05 11:11:16 UTC
I'm trying to return the array "classMarks[]" from the "enterMarks()" method, and assign the array values to the array in main called "marks[]". However I keep getting an error message at line 22, which is the 'return' command. Why is this?

-------------------------------------------------------------
public class Marks {

public static int enterMarks()
{
int[] classMarks = new int[10];

for(int i=0; i {
classMarks[i] = getScannerInput.anInt("Enter the student's marks (out of a possible 20): ");
}
System.out.println("The class exam marks are: ");
for(int j=0; j {
System.out.print(classMarks[j] + ", ");
}

return classMarks;
}

public static void main(String[] args)
{
int[] marks = enterMarks();


}
}

-----------------------------------

By the way, getScannerInput is just a program to simplify the basic operations of JOptionPane for first years of programming, so don't be confused about its existence in the code.

So, help please?

Thanks.
Four answers:
Matthew R
2008-12-05 11:21:57 UTC
The problem is in your method declaration:



public static int enterMarks()



it is looking to return a single int, not an array of ints.



It should say this instead:



public static int[] enterMarks()



//the int[] means you are returning an array
2008-12-05 12:00:17 UTC
Actually, mr "Mehoff," how exactly is Matt wrong? It sounds to me like he is right on track - the method, as is, is set to return a single int, and it needs to return an array of ints. Also, I don't see why the asker needs to know about pointers and such, which are more a topic for C or C++ - when he is obviously programming in Java.



For the purposes needed, I think what Matt suggested is fine. I don't think you would want to - or even can - pass other parameters to the "main" method. And even if you could, it would be bad design practice.



Good luck : )
2008-12-05 11:20:40 UTC
You don't understand what arrays are and how they are laid out in memory, what variable's scope is and what pointers are. So my advice is, don't try to return array from a method. Instead, make sure you understand the basics of the language.



With that said, if you'll want to return the array later on, here's what you do. Allocate the memory for the array in your function and return the pointer to the allocated space. You'll also have to return the size of the array.



This is not advisable, however. It will lead to a bad design. If you want your function to be able to return an array-like structure with variable lengths, use STL.



@Mathew R: Please don't answer questions on the topic that you don't know.
AnalProgrammer
2008-12-05 11:20:34 UTC
Because an array is an object you can pass the array as a parameter to the method and it will be passed by reference and not passed by value.

You can then directly update the array.



Have fun.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...