Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 62 of 62

Thread: ArrayList<> String searches and loops

  1. #51
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    Ok, that is a start. Now add a loop around the testing steps that gets each App one at a time.

    The order of the steps needs fixing: The method gets the rating we're looking for before all the other steps.

    I need to find the apps that have been rated.
    How do you determine that? Is there a special method that says whether or not an App has been rated?

    find out how many apps have the given rating.
    How is that done? What values are tested?

    return the number of apps that are in the store that have a particular rating.
    That should be the last item in the list. It would be outside of the loop that looks at each App one at a time.


    Fix the list of steps as per above and then try writing the code for those steps.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #52
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    To get the rating of an app I need to call the getRating() method. I'm pretty sure I want to use a for-each loop because I need to check each app for a rating.

    There isn't a special method that says if an app is rated that I know of. There are a couple of methods that print out the details of each app, but I don't think that's what we're looking for.

    In order to find out what apps have been rated I think I should compare the parameter with app.getRating().

  3. #53
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    It seems like the logic for this method is the same as for the getAppsByAuthor method. The differences are:
    defined to return a list vs an int
    compares author vs rating
    saves app in list vs adding one to count
    returns list vs the count
    If you don't understand my answer, don't ignore it, ask a question.

  4. #54
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    Yes, this was my thinking. When I was working through this today I couldn't get the rating from get.appRating() to match the given rating parameter of getNumAppsWithRating method. I will work with your tips.

    --- Update ---

    I'm a little confused here. I need to find the apps that are rated and compare them to the parameter. How do I store an integer in an ArrayList? I keep getting an error message that says incompatible types: java util.ArrayList cannot be converted to int. I don't know what ArrayList<Integer> is for, I've never seen it, but is that what I'm supposed to be using?

  5. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    How do I store an integer in an ArrayList?
    Where is the ArrayList listed in the steps that were posted for what this method is supposed to do?
    Go back and look at the steps for what the method is supposed to do. There is nothing about a list.

    That is the reason for making a list of the steps a method is supposed to do. So that you know what is needed and what is not needed. A list is not needed for this method. The only list used in this method is the source of the App objects to be searched.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #56
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    Man, I guess I took your comment "It seems like the logic for this method is the same as for the getAppsByAuthor method." to literal. You didn't actually say that the method would be written the same, you said the logic. Maybe I was hoping, I apologize.

    Here is what needs to be done:

    To get the rating of an app I need to call the getRating() method. I'm pretty sure I want to use a for-each loop because I need to check each app for a rating.

    There isn't a special method that says if an app is rated that I know of. There are a couple of methods that print out the details of each app, but I don't think that's what we're looking for.

    In order to find out what apps have been rated I think I should compare the parameter with app.getRating().

    At least I know the for-each loop will wrap around the code that ultimately produces my answer. I think I got that much. Off to the drawing board.

  7. #57
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    So, I've been trying two different ways to achieve this answer, so far, no luck. We'll see what you say. Since the parameter is the value of the rating we're looking I decided to search the ratings using that number first.
    for (App app : appList) {
        if(rating >= 1 && rating <= 4) {
            app.getRating();
        }
    }

    In an earlier post you mentioned that when I listed what needed to be done I had the order of tasks mixed up. Maybe this corrected some of it...
    Last edited by misterCrypto; August 12th, 2021 at 12:09 PM.

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    Look at the list of steps the method needs to do to solve the problem.
    Testing the range of the rating value passed to the method is NOT in that list of steps.
    The only place a range test would be done is in the App constructor when it is passed the initial value of the rating
    and in the setRating method when a new value is passed to the App class.

    Calling the App class's getRating method without any code to receive or test the value returned does nothing useful.

    The code in post#44 was close to doing what is required. Remove the List and return the value in count after the loop ends.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    Somehow I managed to figure this out...took all night, but it works. Now my problem is if the rating searched is 0. I want it to just error message, not iterate anything. There are no 0 ratings because they are invalid, but there are "not rated." Here's what I came up with.

    /**
     *@locate apps that have been rated 
     *@param represents a rating
     *return the number of apps in store that 
     *have given rating
     *Example: If two apps have a rating of 3, then
     *the call to getNumAppsWithRating(3) returns 2
     */
     
    public int getNumAppsWithRating(int rating)
    { 
       int numAppsWithRating;
       numAppsWithRating = 0;
       for(App app : appList) {
           if(app.getRating() == (rating)) {
               numAppsWithRating++;
           }
       }
       return numAppsWithRating;    
    }

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    Ok, does that work as expected when tested?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList<> String searches and loops

    It does for the most part. The only issue that I have with it is, if the user searches for apps rated 0, it will return the unrated apps. But, if you search for apps with rating above 4, it returns 0. I want to make it so that if the user searches for apps with a rating of 0, an error message pops up and no apps are returned. I've tried this code in numerous spots within this method and it still returns the unrated apps. Suggestions?
     
    for(App app : appList) {
        if(rating == 0) {
            System.out.println("");
        }
    }

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList<> String searches and loops

    if the user searches for apps rated 0
    The place to test the range of values for rating is where the user enters the values to search for, NOT inside of the method.
    Test the value after receiving the input from the user.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    misterCrypto (August 13th, 2021)

Page 3 of 3 FirstFirst 123

Similar Threads

  1. HELP WITH FOR LOOPS! trying to concatenate without using string builder
    By janeeatsdonuts in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 09:00 AM
  2. Write a method that searches the BST for a given input
    By Jurgen in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 6th, 2012, 11:46 AM
  3. Finding String in ArrayList
    By Noob_Programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2011, 05:18 AM
  4. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  5. ArrayList to String/outOfBounds
    By Scotty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2011, 04:41 PM