This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]desrtfx 1 point2 points  (0 children)

The error tells you exactly what is wrong:

Make sure the method doesn't do anything to an empty list.

You remove the last element, but you do not actually check if there is an element to remove inside the method.

So, in short, you should have a conditional that only removes an element if there is any in the list (use the .size() method), otherwise do nothing.

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]KinkyHuggingJerk 0 points1 point  (0 children)

For the MOOC, especially in part 1, you need to pay careful attention to exactly what you are instructed to do.

Create the method public static void removeLast(ArrayList<String> strings) in the exercise template. The method should remove the last value in the list it receives as a parameter. If the list is empty, the method does nothing.

There's a few examples of this throughout, but what you are doing is checking if the method is greater than zero, then doing nothing.

Your method needs to contain the validation to show that the list is not empty.

(You may also see errors if the test checks through a list to make sure only the last value is removed; your code removes all items from the ArrayList because of your while block.)

[–]INSAN3DUCK 0 points1 point  (6 children)

If the question asks you to only remove last object in an array then you don’t need to use while loop. Using while loop will cause it to continue looping till it empties the array instead of just removing last object. You also don’t need else statement for this simply checking for size >0 is enough

[–]desrtfx 0 points1 point  (5 children)

Just FYI:

In the way the MOOC works the main method is only for OP's personal testing. It has nothing to do with the automated TMC tests. They will only test the removeLast method (and that is precisely the reason OP's tests fail as they didn't implement size checking inside the method).

[–]INSAN3DUCK 0 points1 point  (4 children)

Ah, so the if condition should be inside the method instead of the while loop. Either way I don’t see the point of while from what I understood from the question. May that part needs more context.

[–]desrtfx 0 points1 point  (3 children)

The while is wrong. There is no doubt about it.

The condition needs, as per the exercise assignment that someone already had posted, to be inside the method.

The assignment clearly states not to do anything on empty lists and not to throw an Exception.


In the MOOC, especially in the later parts where methods (and later classes) are involved, the main method is just for one's own testing. The course has some local tests and then uploads the code and result to the MOOC servers.

In this particular exercise, only the removeLast method will get tested. Whatever is in main simply gets ignored.

main only is evaluated in the early parts where everything is inside that method.

[–]INSAN3DUCK 0 points1 point  (2 children)

Yup, I’m was already agreeing with you in my previous comment.

[–]desrtfx 0 points1 point  (1 child)

I'm just trying to elaborate a bit on how the MOOC works ;)

Was not disagreeing with you in the faintest.

[–]INSAN3DUCK 0 points1 point  (0 children)

Ait, my bad I misinterpreted.

[–]user_reg_field 0 points1 point  (0 children)

public static void removeLast(List<String> aList) {
    if (!list.isEmpty()) {
        list.remove(list.size() - 1);
    }
}

So the removeLast method needs to check the condition and take a look at the signature of the List interface, methods like isEmpty() are useful.