2

I am writing a program that will enter an array of Students with their name and gpa's and return only the failing students. I am unsure how to return an array that will avoid returning null as an element of that array. I.e. if there are 4 students in the initial array, but only 2 are failing, my array is returning: student1, student2, null, null.

Student Jim = new Student("Jim",1.4);
    Student Tom = new Student("Tom",3.0);
    Student John = new Student("John",4.0);
    Student Bill = new Student("Bill",1.2);
    Student[] group1 = {Jim,Tom,John,Bill};

public Student[] getFailing(Student[] students) {
    int i, j;
    Student[] failing = new Student[students.length];
    Student temp;
    for(i=0, j=0; i< students.length; i++){
            if(students[i].getGpa() < 2.0){
                temp = students[i];
                failing[j] = temp;
                j++;
            }

    }
    return failing;
}

My current result when I do a test run in main is:

name = Jim gpa = 1.4
name = Bill gpa = 1.2
null
null

4 Answers 4

3

If the only problem is printing the nulls, i.e. it's OK if your array has null values but you don't want to see them in your output, then you can leave your method as it is and change your printing code so that it checks for null, and avoid printing them.

But if you must keep null values out of your array, you can use a dynamically resizing data structure like ArrayList, and get an array out of it using the toArray method.

Without using ArrayList, since you're tracking how many Student objects represent failing students with the j variable, after you collect the failing student objects you could create a new array of the desired length, and then fill it with only the non-null students using a loop, or using Arrays.copyOf, or System.arraycopy.

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

4 Comments

System.arraycopy is good, but it's useful to be aware of Arrays.copyOf as well. Under the hood the latter calls the former, but I find it ever so slightly easier to use.
@MichaelMcGowan thanks, good tip. I always forget about Arrays.copyOf
@MichaelMcGowan, is there a way to find the index of null before when copying my array over, so I know the length of my new array? Or is that built in to Arrays.copyOf?
@gotguts you already know that index, or better, the length of the new array: you're keeping track of how many failing Student objects with j.
2

I would recommend for you to use an ArrayList for your program. This allows you to have a variable size array. This way, you don't have to worry about null output. You merely add to it the students you want and the size changes to reflect that.

2 Comments

This is homework. I highly doubt he will be allowed to use an ArrayList since most of the time programming assignment requirements are very rigid. And probably haven't gone over generics yet.
i would do this, but the catch is i can't use an array list for this program. Thanks for the tip though, and I will definitely keep this in mind for future use.
0

You're creating an array that is as long as the original array passed in (which fills all elements with null), but not necessarily filling all the slots. In the given example Tom and John are not failing, so there are two empty (null) slots in your array.

I'm not sure of your programming level, but you might consider using a dynamic array, like java.util.ArrayList, and adding each failing student to the list, then returning the toArray() version.

5 Comments

Is there a way to create the array so it can continue to expand up to the length of the original array?
@gotguts, you cannot expand arrays. Just create new ones.
As I mentioned, an ArrayList, which might be declared ArrayList<Student> failingStudents = new ArrayList<Students>(); would do the trick. You could use failingStudents.add(student); for each failing student. Lastly, you would return failingStudents.toArray();
To OP, this is better: List<Student> failingStudents = new ArrayList<Student>();. Please note the original typo in the second generic type specifier.
Yeah, I did mistakenly put Students instead of Student. I'm not sure if you would want to go with just List, though. In this case, it probably offers the necessary functionality, but the List type itself would probably be losing some methods ArrayList offers (couldn't say off the top of my head).
0

Make the failing array the same size as the amount of failing students. Thus, I would use two, almost identical, loops.

So...

studentsFailed = 0;
while(hasNextStudent()) {
    studentsFailed++;
}
arr = new array[studentsFailed];

Just some pseudocode.

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.