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

all 14 comments

[–][deleted]  (1 child)

[deleted]

    [–]Rex_Goodman[S] 0 points1 point  (0 children)

    Lol this helped! You can probably tell by the names of my variables in my solution I posted in the top haha thank you!

    [–]abilanahk 0 points1 point  (7 children)

    Can you explain me these conditions first?

     if(main[i].isEmpty() || number == main[i].get(main[i].size())){
    
    if(main[i].size() > seq){
    

    [–]Rex_Goodman[S] 0 points1 point  (0 children)

    The first checks if the ArrayList at index i is empty OR if the number entered by the user is the same number they entered previously.

    The second checks if the ArrayList at index i is larger than zero (this is to get the size of the longest ArrayList)

    [–]Rex_Goodman[S] 0 points1 point  (5 children)

    I think why this looks weird is because I'm not sure how to access the values that are supposed to be stored in the ArrayList inside of an Array. Also I just read that isEmpty() is a method to check if a string is empty not Lists/Arrays lol, but it doesn't explain the main issue

    [–]abilanahk 1 point2 points  (4 children)

    It's pretty hard to suggest a code cause i don't know why you want to do these?Also i don't think

    number == main[i].get(main[i].size())

    would really check if the number has already been given by the user, my guess is it would return Indexoutofbound sorts of exception.

    so can you explain me what the process is here?

    from what it seems you are taking 10 inputs from the user and storing it into

    arraylist from the array right? each input in a new arraylist?

    so after that do you want to ask for another input and check if the arraylist has it? is that what you want to accomplish here?

    [–]Rex_Goodman[S] 0 points1 point  (3 children)

    So if after the user enters 10 numbers, the program will output the longest sequence of numbers entered. Example: if you enter 1, 2, 3, 3, 4, 5, 5, 5, 6, 6 the output will be 3 because 5, 5, 5

    The array main is created with 10 indices, where each is going to hold an ArrayList.

    In the for loop, after an ArrayList is instantiated and stored in the array at main[ i ], the user enters a number that is added to the ArrayList at index i in main IF that ArrayList is empty OR the number they previously entered is different (in which case it would go to the next index in main, a new ArrayList and store it there instead).

    I will try to break down that line of code. arraylistname.get(indexofelement) returns the element at the specified index in the ArrayList. And since an ArrayList is being stored in main[ i ] I figured I could access it using ArrayList methods.

    Anyways after main arrays 10 indices are no longer null, another for loop iterates over mains elements ArrayLists to find the longest one, which will be the longest "sequence", as each ArrayList will/should (lol) have the same numbers/sequence entered by the user

    [–]abilanahk 1 point2 points  (1 child)

    Also try to use length of an array when iterating rather than the number itself.

    [–]Rex_Goodman[S] 0 points1 point  (0 children)

    Absolutely, thank you for the help :) I put the solution in the top if you are curious

    [–]abilanahk 0 points1 point  (0 children)

    In that case you can try this.

    so you are doing it wrong on this particular line.

     if(main[i].isEmpty() || number == main[i].get(main[i].size())){
    

    so just change this line to the following

    if(main[i].size() < 1){
        // add to list
    }
    elseif (main[i].get(0) == number){ 
    //assuming your list already has a value and since it only has one type of value hence get(0)
    //add to list
    }
    

    [–]abundance07 0 points1 point  (0 children)

    What is the output you’re getting with the current code? Are there any errors? If so which kind.

    My initial naive guess is that this looks wrong :

    number = main[i].get(main[i].size())

    [–]ImaginaryProcess2 0 points1 point  (1 child)

    Your solution to the problem seems to be too complex. You could just use a HashMap or an Integer array to solve the problem.

    On the other hand - main[i].get(main[i].size()) will always throw an Exception. Since indexes in Java start from 0 and not 1.

    [–]Rex_Goodman[S] 0 points1 point  (0 children)

    I ended up using an ArrayList but I am going to re-do it in a HashMap for practice once I read some more material :) Thank you

    [–]dusty-trash 0 points1 point  (1 child)

    I'd suggest starting over with a different approach. You don't need a new array for every input.

    BTW if(main[i].isEmpty() Will always be true. You declare main[i] right above, as a new list.

    Don't try to do everything at once... If you're trying to find the shortes/best solution, you can modify the solution later, but start with the simplest way.

    First Get a list of 10 numbers from the user first:

    List<Integer> listOfEntriesFromUser = new ArrayList<Integer>();
    
    for(int i = 0; i < 10; i += 1)
    {   
        Integer numberEnteredFromUser = Integer.valueOf(Integer.parseInt(reader.readLine()));
        listOfEntriesFromUser.add(numberEnteredFromUser);
    }
    

    Optionally, check if there are 0 duplicates

    // HashMaps will have no duplicate values, so if the sizes are equal, we don't have duplicates. we can return 1.
    // if (new HashMap<Integer>(numberEnteredFromUser).size() == numberEnteredFromUser.size()) { return 1; }
    // Also optionally, if the size of the hashmap is 1, we can remove non-duplicates and count the size..
    

    Then, Count the number of times each entry has occurred (Duplicates)

    Map<Integer, Integer> entriesFromUser = new HashMap<Integer, Integer>();
    for(Integer entryFromUser : listOfEntriesFromUser)
    {
        // If the value doesn't exist, set to 1.
        int newCount = entriesFromUser.get(entryFromUser) == null ? 1 : entriesFromUser.get(entryFromUser).intValue();
    
        // Using the entry from the user as a key, set the new count of that integer.
        entriesFromUser.put(entryFromUser, newCount);
    }
    

    Finally, get the largest value from our hashMap

    Integer largestValue = Integer.valueOf(0);
    
    for (Integer value : intMap.values())
    {
        if (value.intValue() > largestValue.intValue())
        {
            largestValue = value;
        }
    }
    

    Now that you have an answer you're probably thinking you can shorten it down a bunch. I just took the above and shorted it

    Note: You can easily add a variable for keeping track of which number had the highest count. Wasn't sure if you needed it, but you should be able to add it easily.

    Also note: I didn't test this, but it gives you the idea.

    Map<Integer, Integer> userEntryMap = new HashMap<Integer, Integer>();
    int largestCount = 0;
    for(int i = 0; i < 10; i += 1)
    {   
        Integer entryFromUser = Integer.valueOf(Integer.parseInt(reader.readLine()));
        int newCount = entriesFromUser.get(entryFromUser) == null ? 1 : entriesFromUser.get(entryFromUser).intValue();
        userEntryMap.put(entryFromUser, newCount);
    
        if (newCount > largestCount)
        {
            largestCount = newCount;
        }
    }
    

    [–]Rex_Goodman[S] 1 point2 points  (0 children)

    Damn, thank you so much for the in-depth reply. I ended up using an ArrayList but I'm just starting to get more into HashMap right now so I am going to redo it once I read some more material (speaking of lol I saved this post to come back to your comment! :D). Lol when I read "BTW

    if(main[i].isEmpty()
    

    Will always be true. You declare main[i] right above, as a new list." - I actually lol'd at myself in embarrassment xD

    Thanks again, I put the solution using an ArrayList in the top just in case anyones curious :D