-1

I would like my method public void showClassRoomDetails(String teacherName) to return the Arraylist index number using the teacherName.

Thanks

    import java.util.ArrayList;

public class School
{
private ArrayList<Classroom> classrooms;
private String classRoomName;
private String teacherName;

public School()
{
    classrooms =  new ArrayList<Classroom>();
}

public void addClassRoom(Classroom newClassRoom, String theClassRoomName)
{
    classrooms.add(newClassRoom);
    classRoomName = theClassRoomName;
}

public void addTeacherToClassRoom(int classroomId, String TeacherName)
{
    if (classroomId < classrooms.size() ) {
        classrooms.get(classroomId).setTeacherName(TeacherName);
    }
}

public void showClassRoomDetails(String teacherName)
{
    for (Classroom classroom : this.classrooms)
    {
        if (classroom.returnTeacherName().equals(teacherName))
        {
            System.out.println(classroom.returnClassRoomName());
            System.out.println(classroom.returnTeacherName());   
            break;
        }
    } 
}


}
5
  • 1
    Do you have a specific question? Commented Jan 5, 2011 at 14:54
  • Why did you downvote this? The "question" might be implicit, but clear enough, I guess? Commented Jan 5, 2011 at 14:56
  • 1
    a new user, but an extension of the following 'questions': stackoverflow.com/questions/4599283, stackoverflow.com/questions/4596544, stackoverflow.com/questions/4593232 Commented Jan 5, 2011 at 15:23
  • while many of us are willing to help on homework problems, you should be showing what you have done to try and solve a problem, and asking for specific help. We aren't here to write your homework one method at a time. Commented Jan 5, 2011 at 15:26
  • @RD You're right, I missed those ones... Commented Jan 5, 2011 at 15:37

4 Answers 4

4

Use a regular loop, not the foreach loop:

public int showClassRoomDetails(String teacherName)
{
    for (int i = 0; i < this.classrooms.size(); i++)
    {
        Classroom classroom = classrooms.get(i);
        if (classroom.returnTeacherName().equals(teacherName))
        {
            return i;
        }
    } 

    // Return -1 when the teacher was not found
    return -1;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Either use a regular for loop (for (int i=0;i<classrooms.size();i++)) or use ArrayList.indexOf(classroom).

Comments

0

Something like this?

for (int i = 0; i < classrooms.size(); i++) {
    Classroom classroom = classrooms.get(i);
    if (classroom.returnTeacherName().equals(teacherName)) {
        System.out.println("Index: " + i);
    }
}

Much more elegantly,

public int showClassRoomDetails(String teacherName) {
    for (int i = 0; i < classrooms.size(); i++) {
        Classroom classroom = classrooms.get(i);
        if (classroom.returnTeacherName().equals(teacherName)) {
            return i;
        }
    }

    return -1;
}

Alternatively, keep a counter that gets incremented for each not true condition (on your if statement).

Comments

0
//Return set containing multiple matched indexes
public Set<Integer> showClassRoomDetails(String teacherName){
     Set<Integer> result = new HashSet<Integer>();
     int i=0;
     for (Classroom classroom : this.classrooms){
         if (classroom.returnTeacherName().equals(teacherName))
           result.add(i);
         i++;
     } 
   return result;
 }

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.