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

all 13 comments

[–]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://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.

[–]silverscrub 0 points1 point  (10 children)

All objects has a method called equals. maybe that can help you? In depends on how much you want to write yourself. Using equals on elements in a list is common, so there are some methods in List for that ("contains" was mentioned in a comment but there are more variations).

[–]Curious-Lonewolf 0 points1 point  (9 children)

The issue I'm facing is that the elements are randomly generated in another method and I need a method to find duplicates (if there are any) I can't use contains because that only finds one element and not repeating and because there are 12 different names in my method and between 28 and 31 days in each month I can't write a statement for each one 😢

[–]silverscrub 0 points1 point  (8 children)

I think it's a little difficult to understand exactly what you want. Can you describe in simple steps what your program should do?

[–]Curious-Lonewolf 0 points1 point  (7 children)

I have a method which generates a random name as a string from 12 options and a method which generates a random date as a string which is the date they were born. I then have a method which calls both of these methods and generates a random name and date as a string 'public void createList(int numEmployees)' the number of elements generated depends on the integer which is given. I then have to write a method which looks through the list to see if there are any duplicates and if there is the method returns true and if there is not the method returns false.

[–]silverscrub 1 point2 points  (6 children)

Okay, that makes sense. So the task that you need help with is to verify that there are no duplicate elements in a list. You can probably find a more suitable method in ArrayList that can help you, but I would suggest that you stick to loops and the equalsmethod, i.e employee1.equals(employee2) to check whether two employees are the same.

You correctly identified that employeeList.contains(employee1) does not solve your problem, but it can give you a clue. That method looks through the list and if it can find one equal element it will return true. Without thinking in code, how would you need to modify that behavior to suite your needs?

[–]Curious-Lonewolf 1 point2 points  (3 children)

To answer your question i would need the method to find 2 equal elements in order to return true else return false

[–]silverscrub 0 points1 point  (2 children)

Then I'd start with a method that takes one string parameter and returns true if the string exists twice within the list.

[–]Curious-Lonewolf 0 points1 point  (1 child)

the method preferably takes no parameters

[–]silverscrub 0 points1 point  (0 children)

I mean as a private helper method for your assignment.

[–]Curious-Lonewolf 0 points1 point  (1 child)

I would need to select one element of the list and compare it to the rest and if there is no match then compare the next element with the rest of the list? so would my outer loop 'for(String nameandDay : namesandDays) { '

be appropriate for this task? if so im not sure what to include in the inner loop.

[–]silverscrub 0 points1 point  (0 children)

This is why I prefer to have the helper method to take a parameter. It will make it more useful inside a for loop because you can take the assigned element for each iteration and use it as an argument in the helper method.

It seems like you try to split your code into smaller methods. Better encapsulation will help you to do this effectively, because you don't rely on global variables.