-2

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.

Renee
  • 13
  • 3
  • 3
    If you explain the purpose of the five-element array, we might be able to give you some options. – John Sensebe Jan 29 '16 at 17:45
  • @AMACB The asker is specifically trying to avoid using a multidimensional array. – John Sensebe Jan 29 '16 at 17:46
  • It would help a lot if you could explain what you think this statement does: `double [] names[i] = new double [5];`, specifically what you understand `double` to mean. – Jim Garrison Jan 29 '16 at 17:55
  • 1
    @JimGarrison The asker thinks that it would define an array of `double` named by the value of `names[i]` (i.e. `"Ann"` -> `Ann[]`). – John Sensebe Jan 29 '16 at 17:59
  • 1
    So what is really required is a `Map`, or more likely a `Map` for some appropriate definition of `Student`. – Jim Garrison Jan 29 '16 at 18:01
  • @JohnSensebe the purpose of the line double [] names[i] = new double [5]; was to use the names stored in the names array to create a new array of length 5 that would then store each student's grade from 5 different classes. The reason why I'm using a double is because I'll have to average it later, and it will prevent loss of data. – Renee Feb 02 '16 at 15:29
  • I already understood all of that except for the purpose of the five-element array, which wasn't in your original question. The only thing remaining is why is this marked as a duplicate? The question specifically excludes multidimensional arrays and the accepted answer doesn't contain one. – John Sensebe Feb 02 '16 at 17:36
  • Originally I also asked that if no other solution existed without using a multidimensional array to explain how to create one within the program, which is likely why they flagged it. I've edited it now but they haven't taken the flag down yet. – Renee Feb 05 '16 at 15:57

2 Answers2

2

To do this without multi-dimensional arrays is by creating an Array of Students class, which would hold information on the student, such as firstName, lastName, grade, etc.

Student class:

public class Student(){

    String fname, lname;
    int grade;

    public Student(String name){
        String[] firstLast = name.split(" ");
        fname = firstLast[0];
        if(firstLast.length>1) lname = firstLast[1];
    }

    public string setFName(String nameOfStudent){
         fname = nameOfStudent;
         return fname;
    }

// rest of code implementation
}

In your current class:

Student array[] = new Student[NumberOfStudents];

then you can use the idea that you already have

for (int i = 0; i < NumberOfStudents; i++){
    System.out.print("What is student number " + (i+1) + "'s name?: ");
    String studentName = reader.nextLine();

    array[i] = new Student(studentName); // initialize the array 
}
Calvin P.
  • 1,116
  • 2
  • 8
  • 13
Kevin Mee
  • 539
  • 3
  • 14
  • 1
    This is essentially what I had in mind for an answer. However, don't forget that after initializing the array, you still need to initialize each element in the array. Your code will currently throw `NullPointerException`s – Calvin P. Jan 29 '16 at 17:56
  • Good catch, I updated the answer @CalvinP. – Kevin Mee Jan 29 '16 at 17:58
  • Also missing a constructor. In OP's case, they would probably want one that takes String argument for name (or 2 Strings for first,last) – Calvin P. Jan 29 '16 at 18:01
  • @KevinMee So basically just make another class that would assist in storing the Student's names and make those into individual arrays? I think I can manage that. Thank you! – Renee Feb 02 '16 at 15:38
0

It looks like you are trying to create an array using the string just entered as the variable name, like this:

double[] <student_name> = new double[5];

Unfortunately (or perhaps fortunately), you cannot create a variable from the contents of another.

Instead, you can do one of the following:

  • Do what Kevin Mee has suggested in his answer and use a Student class.
  • Use Map<String, Double[]>, like Jim Garrison suggested in the comments on your question.
  • Use a 2 dimensional array.

If you want to try a 2 dimensional array you should; in your current class, define a 2 dimensional array.

double[][] studentInfo = new double[NumberOfStudents][5];

Then you can reference the array like this:

studentInfo[i][j] = aDoubleNumber;
Tiz
  • 680
  • 5
  • 17