0

in my program there are three class Student,School and TestStudent. I have declared students state inside student class and also there are methods for getting students subject,i have created an array list of type student in School class,but when i try to access student's method in school i get error newStudent type can not be resolved.Here are my codes.

public class Student {
        String name;
    String subject;
    int age;
    Student(String name,String subject,int age){
    this.name = name;
    this.subject = subject;
    this.age = age;
}
public void setName(String name){
    this.name = name;
}
public String getName(){
    return this.name;
}
public String getSubject(){
    return this.subject;
}
public int getAge(){
    return this.age;
}

}

public class School {
public ArrayList <Student> students = new ArrayList <Student>(); 
public void addStudent(String name,String subject,int age){
    Student newStudent = new Student(name,subject,age);
    students.add(newStudent);
}
public void showSubject(String student){
     newStudent.getSubject();


}

} 
1
  • changed the code using rohit's suggestion.now its working. Commented Jan 6, 2013 at 10:37

3 Answers 3

4
newStudent.getSubject();

This is not what you want. Because you haven't retrieved that student yet from the ArrayList.

You would need to iterate over the ArrayList, and see which student have the name as passed in parameter. So, just use a for-each loop to iterate over your ArrayList, and return appropriate Student.

So, your method should look like: -

public void showSubject(String student){
     for (Student student: students) {
        if (student.getName().equals(student)) {
            System.out.println(student.getSubject());
        }
     }
}

Note that, using a Map here would be a better idea as explained by @Peter in his answer.

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

Comments

2

If you want to look up a student by name, I would make the name of a student immutable and use a Map<String, Student> instead. This would allow you to write

 Student student = map.get(studentName);

Using a List, you would have to search through every entry as Rohit suggests. Note: it is possible to have multiple students with the same name (as in real life) with a List.

2 Comments

Umm. Why have you explicitly specified making name of the student immutable? Since it's a String, it is implicit.
The Student name; should be final so it can be safely used as a key.
1

newStudent is a local variable in addStudent(), hence you cannot access it from showSubject().

I don't know what should be your logic of accessing the subject, but, for example, it could be accessed via students.get(0).getSubject() [provided the list is not empty]

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.