1

I am getting an error cannot convert int to int[], can anyone help please?

//Create array
int [][] studentResults = new int [numStudents][numExams];

//Fill 1st dimension with Student numbers 1 through numStudents
for (int count = 0; count < numStudents; count++)
    studentResults[count][] = count + 1;
2
  • 2
    What exactly are you trying to accomplish? Commented Jun 25, 2013 at 2:10
  • Trying to create 2 dimensional array. Fill first with a number of students and the second with user input exam scores. User can check on final grade Commented Jun 25, 2013 at 2:13

2 Answers 2

1

In Java, if you want to assign a value to an entry in an array, you need to specify all of the instances for the array. I would suggest the following:

//Create array
int [][] studentResults = new int [numStudents][numExams];

//This loops through the two dimensional array that you created 
//And fills the 1st dimension with Student numbers 1 through numStudents.
for (int count = 0; count < numStudents; count++)
    for (int exam = 0; exam < numExams; exam++)
        studentResults[count][exam] = count + 1;

thereby iterating through every exam entry of studentResults for each student.

Sign up to request clarification or add additional context in comments.

1 Comment

no need to set the other column value.. only need to set the first column
1

So you need to set the value of first column of each row. As we know the first column index is 0. So for each row set the 0 column of the array like this

for (int count = 0; count < numStudents; count++)
    studentResults[count][0] = count + 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.