How do I pass an array from my main method to another method? I'm having an error with the parameters. Do I use the return value from main? And since the return value from main is an array, should the parameters for the call of main have brackets? Should there be a value between those brackets?
public class arraysAndMethods {
public void printArray(double[] arr) {
int x = arraysAndMethods.main(double[i] arr);//error with paremeters
for (int i = 0; i < studGrades.lenght; i++)
System.out.print(studGrades[i] + " ");
}// end of printArray method
public static double[] main(String args[]){// double array
java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
System.out.println("What is the size of the class?");
int n = input.nextInt();
double[] arr = new double[n];// declare and initialize array to have n many elements
for (int i = 0; i < arr.length;i++) {// input grades for each students
System.out.println("What is the grade of student #" + (i+1));
arr[i] = input.nextDouble();
} // end of for loop
return arr;
}// end of main method
}// end of class
Your main method is where the program begins execution. In other words you should be calling printArray() from within main, not the other way around
This whole thing doesn't make much sense. If you're hoping to have this as the
main
method of a Java program, this won't work because themain
method must have avoid
return type. Regardless, you have a syntax error here:Instead it should be
as the variable has already been declared. I'm not sure what you were trying to do with
double[i]
, but that syntax is more like a declaration, i.e.Just pass the name, not the type.
EDIT: Besides that, your code shows a few other problems.
I think I see where the confusion is. Perhaps this will help.
First I think you should understand something very important about Java.
Your main method is the FIRST method that is run.
So let's give a short walk through of how a Java program is created without an IDE. First, you write your code in a text file and rename it to arraysAndMethods.java when you're done. Next we need to take your code and put it into a form that is usable by the computer it is first compiled down to bytecode with the following from the command line:
javac arraysAndMethods.java
After it is compiled you can run the program with this command:
java arraysAndMethods
Your program will run if there are no problems. You say that you want to pass variables things into the main method? Here is how you do that from the command line:
java arraysAndMethods 45.6 38.2 5.5 105.3
Looking at your main method, it takes the following arguments:
(String args[])
So in your case, the
45.6 38.2 5.5 105.3
would be passed into your main method as an array of strings. With the first entry being45.6
, followed by38.2
, then5.5
and finally105.3
. It would look like this in the array:["45.6"],["38.2"],["5.5"],["105.3"]
but they are all Strings and not doubles.All of this is to say that if you want to pass something in to your main method, you'll either need to do it through the command line, or look up how your individual IDE (i.e. Eclipse, Netbeans, etc.) handles that.
So to recap: The parameters in the main method are coming in from the console unless other specifications are made, and in your case it is returning an array of type double.
I know this is quite verbose, but bear with me, I'm almost done.
When a Java program is run(I'm simplifying a bit here), it comes into the main method and executes the first line it sees. When it's done with that one, it goes onto the next line, and so forth until it gets to the end of the main method. Then the program is done.
So everything MUST be done in the main method. Although you can call other Classes and methods from the main method.
Now that you have the explanation, here is what I would do to fix your code:
Please be sure to mark the best answer when you're ready and up-vote any answers or comments you feel helped. Not just for this question, where you are the original poster, but for other question threads as well.