How do I return an array to the main method. I just want to know how to return an array that declared in sub method.
I have tried this program. What I did here is get 10 of subject marks to a double type array. I want to return that array to main method to get total and average from another method.
<code>import java.util.*;
class Main {
public static void getMarks(){
Scanner scanner = new Scanner(System.in);
int[] marks = new int[10];
for (int i = 0; i < 10; i++) {
System.out.print("Enter mark for subject " + (i + 1) + ": ");
marks[] = scanner.nextInt(); //Main.java:11: error: not a statement
//Main.java:11: error: ';' expected
}
return marks;
}
public static void main(String args[]){
double[] marks = getMarks();
}
}
</code>
<code>import java.util.*;
class Main {
public static void getMarks(){
Scanner scanner = new Scanner(System.in);
int[] marks = new int[10];
for (int i = 0; i < 10; i++) {
System.out.print("Enter mark for subject " + (i + 1) + ": ");
marks[] = scanner.nextInt(); //Main.java:11: error: not a statement
//Main.java:11: error: ';' expected
}
return marks;
}
public static void main(String args[]){
double[] marks = getMarks();
}
}
</code>
import java.util.*;
class Main {
public static void getMarks(){
Scanner scanner = new Scanner(System.in);
int[] marks = new int[10];
for (int i = 0; i < 10; i++) {
System.out.print("Enter mark for subject " + (i + 1) + ": ");
marks[] = scanner.nextInt(); //Main.java:11: error: not a statement
//Main.java:11: error: ';' expected
}
return marks;
}
public static void main(String args[]){
double[] marks = getMarks();
}
}
New contributor
Gamitha Gimhana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2