-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.

8
  • 3
    If you explain the purpose of the five-element array, we might be able to give you some options. Commented Jan 29, 2016 at 17:45
  • @AMACB The asker is specifically trying to avoid using a multidimensional array. Commented Jan 29, 2016 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. Commented Jan 29, 2016 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[]). Commented Jan 29, 2016 at 17:59
  • 1
    So what is really required is a Map<String,Double[]>, or more likely a Map<String,Student> for some appropriate definition of Student. Commented Jan 29, 2016 at 18:01

2 Answers 2

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 
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 NullPointerExceptions
Good catch, I updated the answer @CalvinP.
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)
@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!
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.