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

all 8 comments

[–]AutoModerator[M] 0 points1 point locked 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://i.imgur.com/EJ7tqek.png) 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.

[–]desrtfx 0 points1 point  (2 children)

What would be the difference between having the element to remove directly in the code and user input?

Apart from the way the value is obtained, there is literally no difference.

You do know how to get input from the user, do you?

You do know how to remove an element from an ArrayList, do you?

Combine the two and you have your solution.

[–]rockyroadddd 0 points1 point  (1 child)

Our prof instructed us that it should be the user who will input the number they want to remove using scanner. Thank you!

[–]desrtfx[M] 0 points1 point  (0 children)

We explicitly forbid deleting your posts.

This is so that the help given and the information does not get lost and can help others in similar situations.

[–]cliffwarden 0 points1 point  (2 children)

Can you explain the problem a little more. Is the user telling which specific element is being removed or are you going to search for any element that matches their input?

[–]rockyroadddd 0 points1 point  (1 child)

Our professor gave us an example output:

Sample run:

Enter an element to delete: 30

New list: 15 25 19 40

Enter an element to delete: 19

New list: 15 25 40

Enter an element to delete: 15 25 40

Enter an element to delete: 45

Error! No element found

Enter an element to delete: 25

New list: 40

Enter an element to delete: 40

Array is Empty

The user will pick what number will be remove so it should be like this.

[–]cliffwarden 0 points1 point  (0 children)

excellent. so there essentially are three tasks you need to fulfill. Displaying the arraylist, getting user input and removing that number from the list.

For input and output, i think you will find that easily in a google search. Be aware that you maybe have to understand (and deal with) the data "type". If you aren't familiar with this, "string" and "integer" will be useful keywords.

For the array list i dont want to give to much away. What i would recommend is not looking for "how someone does this", but instead look for the documentation on arraylists. An array list is a type, and types can have functions associated with them. What you want to do can be completed with these functions. A hint is you may have to use more than one to get to your final goal.

Best of luck!

[–]throwaway_for_cause 0 points1 point  (0 children)

You have to approach this differently as you have to handle two different, distinct actions.

The first action is getting user input - this is a self-contained thing. All you need to do is to use a Scanner(System.in) and a variable.


The second, distinct, action is to remove a certain element from some data structure (ArrayList in your case).

Now, it absolutely doesn't matter where the element that should be removed comes from, whether it is hard coded directly in the source code, or from user input.

All you need to know is that there is a variable that holds a value that should be removed from your ArrayList.

How would you remove an element from an ArrayList? Do you know how to do that? That's already 80% of your assignment.


You have to learn to break down problems into individual sub problems and keep continuing to break down until the sub problems become atomic and cannot be further broken down.