How do i print out female only students from an array. My current code prints out all students. the gender variable is boolean (male = true, female = false).
public class SMSMain {
/**
*
* @param args
*/
public static void main(String[] args) throws IOException {
Student student[] = new Student[3];
// Create an instance of student object
//set different attributes of the individual student.
student[0] = new Student();
student[0].setNewId(10);
student[0].setName("Maria");
student[0].setGender(female);
student[1] = new Student();
student[1].setNewId(11);
student[1].setName("Mark");
student[1].setGender(male);
student[2] = new Student();
student[2].setNewId(12);
student[2].setName("Denise");
student[2].setGender(female);
System.out.println("\n\nFemale students are:");
for(int i=0; i < student.length; i++){
System.out.println( "Student " + (i+1) + " Name :: " + student[i].getName() + ", Student ID :: " + student[i].getIdNumber());
something like the following would work
This code runs through every object in the array, checking whether the value returned by getGender is set to false (aka female). If this condition is true, the print statement is executed.
btw, as pointed out by other posters, the use of the name Gender is quite misleading here, a method like
isMale()andisFemale()that would return boolean based on the gender of the person would be a better solution.