user6084102
user6084102

Reputation:

Adding object variable to array

I want to insert name variable of these objects to studentarray with addStudent() method.I used get method but it gives me error.Where is my mistake?Here's what i tried:

public class Student {
    String name;
    int grade;
    public Student(String name, int grade) {
        this.name=name;
        this.grade=grade;
    }
    public String toString() {
        return "name:"+this.name+"grade:"+this.grade;
    }
    public String getName() {
        return name;
    }
}

public class Course {
    Student[]studentArray=new Student[3];

    public Course() {}

    public void addStudent(Student s) {
        for (int i=0;i<studentArray.length;i++) {
            if (studentArray[i] == null) {
                studentArray[i] =s.getName();
                break;                       
            }
        }       
    }
}

Upvotes: 0

Views: 89

Answers (4)

user6084346
user6084346

Reputation:

1.You cannot have two public classes in one file. 2. You cannot convert String to Student object. 3. When you did Student[]studentArray=new Student[3], you are not creating objects but 3 references.It is as same as doing Student studentArray three times where studentArray is just a reference. 4.studentArray[i].getName(); is the right way to call a method using array of objects.

public class Student {
String name;
int grade;
public Student(String name, int grade) {
    this.name=name;
    this.grade=grade;
}
Student(){

}
public String toString() {
    return "name:"+this.name+"grade:"+this.grade;
}
public String getName() {
    return name;
}
}

 class Course {

 public static void main(String [] args) {
 Student[]studentArray=new Student[3];
 for( int i =0; i < studentArray.length; i++ ) {
    studentArray[i] = new Student();
  }

 for (int i=0;i<studentArray.length;i++) {
        if (studentArray[i] == null) {
            studentArray[i].getName();
            break;                       
        }
    }       
} 
}

Consider accepting the answer if it helped you.I will highly appreciate.

Upvotes: 1

Chris Gong
Chris Gong

Reputation: 8229

I'm assuming the error is occurring at this line

studentArray[i] =s.getName();

because you're trying to assigning data of type String to a variable of type Student. You can fix this by simplying assigning the student object in the parameter to the empty index in studentArray. Here is what it should like,

 public void addStudent(Student s) {
        for (int i=0;i<studentArray.length;i++) {
            if (studentArray[i] == null) {
                studentArray[i] = s
                break;                       
            }
        }       
 }

Upvotes: 0

Cootri
Cootri

Reputation: 3836

Give it a try:

studentArray[i] = s;

Instead of:

studentArray[i] = s.getName();

Because your array stores Student type values, not their names (String class instances)

Or you can change your array type to String:

String[] studentArray = new String[3];

Instead of:

Student[] studentArray = new Student[3];

Upvotes: 0

Pushpendra Pal
Pushpendra Pal

Reputation: 640

Your array type is Student and you are trying to store a String, name of the student that is why it is giving error.

Upvotes: 0

Related Questions