FatimahFatCakes
FatimahFatCakes

Reputation: 331

How to return null when condition is not found?

I'm trying to get a return null when my method does not find what its looking for. The method is looking for the most recent submission before the deadline (the parameter). Can someone help me how to get this when there is no submission before the parameter date. Thanks!

    // students is a LinkedList

    @Override
    public Submission getSubmissionBefore(String unikey, Date deadline) {
        // TODO Implement this, ideally in better than O(n)

        if (unikey == null || deadline == null) {
            throw new IllegalArgumentException("Unikey or deadline was null");
        }

        Date tempDate = null;
        int k = -1;

        for (int i = 0; i < this.students.size(); i++) {
            if (students.get(i).getUnikey().equals(unikey)) {
                if (tempDate == null) {
                    tempDate = students.get(i).getTime();
                    k = i;
                } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                    k = i;
                }
            }
        }

        return students.get(k);
    }

Upvotes: 2

Views: 361

Answers (5)

Junaid Ahmed
Junaid Ahmed

Reputation: 144

You can compare current date and return null with if statement

    Date current = new Date();
    if(current.before(deadline))
           return null;
    else{  for (int i = 0; i < this.students.size(); i++) {
        if (students.get(i).getUnikey().equals(unikey)) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                k = i;
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                k = i;
            }
        }
    }

    return students.get(k); 
                   }

Upvotes: 1

Schidu Luca
Schidu Luca

Reputation: 3947

You can also solve the problem in a functional way, with streams (of course if you are using java 8).

public static Submission getSubmisionBefore(String unikey, Date deadline) {
    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Optional<Submission> any = students.stream()
            .filter(submission -> submission.getUniKey().equals(unikey) && submission.getTime().before(deadline))
            .max(Comparator.comparing(Submission::getTime));

    return any.orElse(null);

}

Upvotes: 3

tobias_k
tobias_k

Reputation: 82929

There are a few problems with your code:

  • your first if condition is wrong; it will match any submission with matching unikey, even if it's after the deadline
  • your second else if is also wrong, as you set k, but do not set tempDate
  • before return, you should check whether k is still -1, e.g. using a ternary return k == -1 ? null : students.get(k); however:
  • instead of iterating the indices, you can iterate the Submissions directly using a for-each loop, making the entire code a lot more compact
  • by storing the result itself, you need neither k nor tempDate and do not have to check whether the index is still -1 at the end

Using above advice, you can simplify the loop to this:

Submission res = null;
for (Submission sub : this.students) {
    if (sub.getUnikey().equals(unikey)) {
        if (sub.getTime().before(deadline) && 
                (res == null || sub.getTime().after(res.getTime()))) {
            res = sub;
        }
    }
}
return res;

Or this more compact form, using Java 8 Streams:

return this.students.stream()                        // iterate all students
        .filter(s -> s.getUnikey().equals(unikey))   // get entries with matching key
        .filter(s -> s.getTime().before(deadline))   // get entries before deadline
        .max(Comparator.comparing(s -> s.getTime())) // from those, get the latest one
        .orElse(null);                               // or null if none exists

Upvotes: 3

UMKalaycı
UMKalaycı

Reputation: 83

you can do it by adding controls

@Override
        public Submission getSubmissionBefore(String unikey, Date deadline) {
            // TODO Implement this, ideally in better than O(n)

            if (unikey == null || deadline == null) {
                throw new IllegalArgumentException("Unikey or deadline was null");
            }

            Date tempDate = null;
            int k = -1;

            for (int i = 0; i < this.students.size(); i++) {
                if (students.get(i).getUnikey().equals(unikey)) {
                    if (tempDate == null) {
                        tempDate = students.get(i).getTime();
                        k = i;
                    } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                        k = i;
                    }
                }
            }
            if(k!=-1)
            return students.get(k);
            else
            return null;
        }

Upvotes: 1

public Submission getSubmissionBefore(String unikey, Date deadline) {
    // TODO Implement this, ideally in better than O(n)

    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Submission sub = null;
    Date tempDate = null;

    for (int i = 0; i < this.students.size(); i++) {
        if (unikey.equals(students.get(i).getUnikey())) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                sub = students.get(i);
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                sub = students.get(i);
            }
        }
    }

    return sub;
}

Upvotes: 1

Related Questions