I'm trying to create an array that will allow me to use the element stored to create another separate array.
Here's what I have so far:
Scanner reader = new Scanner(System.in);
//variables
int NumberOfStudents;
System.out.print("How many students are in the class?: ");
NumberOfStudents = reader.nextInt();
reader.nextLine();
//objects
String [] names = new String[NumberOfStudents]; //Creates an array based on the number of students
//input
for (int i = 0; i < NumberOfStudents; i++){
System.out.print("What is student number " + (i+1) + "'s name?: ");
names[i] = reader.nextLine();
double [] names[i] = new double [5]; //declares each student name as a separate array
}
In this, I have the line double [] names[i] = new double [5];
, which should take the value of the names[]
array at the index i
and turn it into an array of length 5. So if names[1] = Ann
, it should create an array Ann[]
with length 5. However, it throws an illegal start of expression error.
I attempted to use a temporary variable to assist in declaring the multiple arrays too, however I gained more errors alongside the illegal start of expression.
So apparently you can't use arrays or variables to declare other arrays.
Is there any way to fix this without using a multidimensional array?
Thanks in advance.