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 1 of 3 123 LastLast
Results 1 to 25 of 62

Thread: ArrayList<> String searches and loops

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

    Default ArrayList<> String searches and loops

    I tried to leave all the header signatures as is and the javadoc sections above them so you can see what I'm trying to do here. What I'm really struggling with is, well, everything! I can't seem to get the syntax correct when I'm trying to search an ArrayList<App> appList for a String, using a String. I need to search for specific String names using a String name. I'm not sure how you're actually supposed to say that. I might be looking for String variables with String objects or the other way around or not at all. I hate Strings. I can't figure out how to search a list for "Television" using the word "Television" I've tried string1.equals(string2) but obviously don't know how to properly write it.
    I tried String string1 = new String name I've tried countless different variations and I can't seem to get it. I need a new teacher is a
    String string2 = *method call to getName()* good start. I've got books, notes, websites, and they all help in one way or another, but
    int diff = string1 - string2; when it comes to applying the lessons to real programs, the syntax doesn't work anymore,
    you have to call methods, objects, other classes, and books don't have that. They have one
    or two line examples. Any help is GREAT help, thanks all.
    import java.util.ArrayList;                // There is another class that goes with this one called App. I'll include it on the bottom.                                
    import java.util.Scanner;
     
    public class AppStore
    {
        private String appStoreName;
        private ArrayList<App> appList;
     
        /**
         * Create an app store with the given name
         * @param name the name of this app store
         */
        public AppStore(String name)
        {
            appStoreName = name;
            appList = new ArrayList<App>();
        }
     
        /**
         * Populate the store with a few apps.
         * Use this method to make testing easier. After creating an AppStore,
         * call this method to populate the apps, and then test your methods.
         */
        public void populateApps()
        {
            addApp("Pandora Music", "Pandora", 0);
            addApp(new App("Minecraft", "Mojang", 6.99, 3));
            addApp(new App("Coinbase", "Coinbase Global Inc.", 0, 0)); 
            addApp(new App("Telegram Messenger", "Telegram FZ LLC", .99, 0));
            addApp(new App("IPTV Extreme", "Paolo Turatti", 1.99, 0));
     
        }
     
        /**
         * Add the given app to the app store
         * @param anApp an app to add
         */
        public void addApp(App anApp)
        {
            appList.add(anApp);
        }
     
        /**
         * Create an app with the given name, author, and price and add it to the store.
         * The app starts out unrated.
         * @param name name of the app
         * @param author the app author
         * @param price the price of the app
         */
        public void addApp(String name, String author, double price)
        {
            appList.add(new App(name, author, price));
        }
     
        /**
         * @return the number of apps in the store
         */
        public int getNumberOfApps()
        {
            return appList.size();
        }
     
        /**
         * Removes all the apps from the store
         */
        public void clearAppStore()
        {
            appList.clear();
        }
     
        /**
         * Print all the apps in the store
         */
        public void printAppList()
        {
            System.out.println("============= " + appStoreName + " =============");
            if (appList.size() == 0) {
                System.out.println("No apps in the store");
            }
            else {
                for (App currentApp : appList) {
                    currentApp.printAppInfo();
                }
            }
            System.out.println("===========================================");
        }
     
        /**
         * Find an app by name
         * @param the name of the app being looked for
         * @return the name of the app if found
         */
        public App findApp(String name)
        {
            populateApps();
            String name1 = new name;
            String name2 = appList.getAppName();
            int diff = name1.compareTo(name2);
            for(App app : appList) {
                if(diff <= 0) {
                    System.out.println("App was found.");
                }
                return null;
            }
     
        }
     
        /**
         * @remove an app from the App store
         * @param is name of app to remove
         */
        public void removeApp(String name)
        {
            populateApps();
            int index = 0;
            for(App app : appList) {
            }
        }    
     
        /**
         * @locate an app by the author name
         */
        public App getAppsByAuthor(String name)
        {
            populateApps();
            ArrayList<App> author = new ArrayList<App>();
            for(App app : appList) {
                String appAuthor = app.getAuthor();
                if(app.getAuthor().equals(null)){
                     return null;
                }
                return author;
            } 
        }
     
        /**
         *@locate apps that have been rated 
         */
        public void getNumAppsWithRating(int rating)
        {
     
        }
     
        /**
         * @prints a summary of the app store
         *  prints total number of apps, name of store,
         *  number of apps with each rating and unrated apps
         */
        public void printAppStoreSummaryStats()
        {
     
        }
     
    }
     
    public class App
    {
        private String name;
        private String author;
        private double price;
        private int rating; // valid ratings are 1-4 or 0 meaning not rated
     
        /**
         * Create an app with the given name, author, price, and rating
         * @param appName the app name
         * @param appAuthor the app author
         * @param appPrice the price of the app (0 if the app is free)
         * @param appRating the app's rating
         */
        public App(String appName, String appAuthor, double appPrice, int appRating)
        {
            name = appName;
            author = appAuthor;
            price = appPrice;
            rating = appRating;
        }    
     
        /**
         * Create an app with the given name, author, and price that is not rated
         * @param appName the app name
         * @param appAuthor the app author
         * @param appPrice the price of the app (0 if the app is free)
         */
        public App(String appName, String appAuthor, double appPrice)
        {
            name = appName;
            author = appAuthor;
            price = appPrice;        
        }
     
        /**
         * @return the app name
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * @return the app author
         */
        public String getAuthor()
        {
            return author;
        }
     
        /**
         * @return the app price
         */
        public double getPrice()
        {
            return price;
        }
     
        /**
         * Set the price of this app to the value given
         * @param newPrice new price for this app
         */
        public void setPrice(double newPrice)
        {
            if (newPrice >= 0) {
                price = newPrice;
            }
            else {
                System.out.println("Error: Price must be greater than or equal to 0");
            }
        }
     
        /**
         * @return true if this app is free, false otherwise
         */
        public boolean isFree()
        {   
            if(price == 0) {
                return true;
            }
            else {
                return false;
            }
        }
     
        /**
         * @return the app rating
         */
        public int getRating()
        {
            return rating;
        }
     
        /**
         * Set the rating of this app to the value given
         * @param newRating new rating for this app
         * Valid rating is 1-4
         */
        public void setRating(int newRating)
        {
            if((newRating >= 1) && (newRating <= 4)) {
                rating = newRating;
            }
            else if((newRating == 0) || (newRating > 4)) {
                System.out.println("Error: Valid rating range is 1-4.");
            }
        }
     
        /**
         * Reset the rating of this app to not rated
         */
        public void resetRating()
        {
            rating = 0;
        }
     
        /**
         * Increase the rating of this app by 1
         */
        public void increaseRating()
        {
            if(rating >= 0 && rating <= 3) {
                rating = rating + 1;
            }
            else if(rating == 0 || rating >= 4) {
                rating = rating;
            }
        }
     
        /**
         * Decrease the rating of this app by 1
         */
        public void decreaseRating()
        {
            if(rating == 1) {
                rating = 1;
            }
            else if(rating >= 2 && rating <= 4){
                rating = rating - 1;
            }
            else if(rating == 0){
                 rating = rating;
            }
        }
     
        /**
         * Print information on this app
         */
        public void printAppInfo()
        {
            System.out.println("---------------------------------");
            System.out.println("App: " + name);
            System.out.println("Author: " + author);
            if(price == 0) {
                System.out.println("Price: FREE");
            }
            else{
            System.out.println("Price: $" + price);
            }
            if(rating == 0) {
                System.out.println("Rating: (not rated)");
            }
            else{
            System.out.println("Rating: " + rating);
            }
            System.out.println("---------------------------------");

  2. #2
    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

    The posted code is missing some parts and will not compile for testing.
    There is no main method for executing when the code does compile.

    If you are trying to find the object in a list that contains a String, think of it this way:
    There is a line of cars that contain things like jackets. As the cars in the line drive past you,
    you ask the driver of each car if his car contains a blue jacket. You do not look at the car to see if it is the blue jacket. The blue jacket is inside the car and there needs to be a way for you to ask if the car contains the jacket.

    In the program, there is a list of App objects (cars). Some of the App objects contain a String (blue jacket).
    The code needs to ask each of the App objects if it contains the desired String.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    That is everything. I can get it to compile aside from my poor method writing in the AppStore Class. App Class works. Want me to send the original assignment source code?

  4. #4
    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

    That is everything.
    No, there is missing code. For example the ending }s are missing.
    And there is no main method.

    Did you understand my analogy about a line(ArrayList) of cars(App objects) that contain things like jackets(String)? You must ask the driver of the car if his car contains the desired jacket.

    --- Update ---

    the original assignment source code
    What code was provided and what code are you writing?
    Can you add comments to the code to designate what code you are writing? For example:
      //  Following code is mine
        ....  your code here
       // End of my code
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Ok, here's my first assignment. Create an App. No java packages on this, just standard accessor and mutator methods. Everything on here runs and works, nothing wrong here.

    /**
     * An app for a mobile device
     * 
     * Modifications:
     * CT: Added validation to setPrice
     * TS: Added isFree method
     * TS: Added validation to setRating
     * TS: Added setRating method
     * TS: Updated print method for appRating and appPrice
     * TS: Added boolean isFree method
     * TS: Added methods for increase/decrease appRating
     * TS: Updated appRating conditions
     * TS: Updated conditions of decrease appRating
     * TS: Updated conditions of increase appRating
     * TS: Added external method call to constructor
     
    public class App
    {
        private String name;
        private String author;
        private double price;
        private int rating; // valid ratings are 1-4 or 0 meaning not rated
     
        /**
         * Create an app with the given name, author, price, and rating
         * @param appName the app name
         * @param appAuthor the app author
         * @param appPrice the price of the app (0 if the app is free)
         * @param appRating the app's rating
         */
        public App(String appName, String appAuthor, double appPrice, int appRating)
        {
            name = appName;
            author = appAuthor;
            price = appPrice;
            rating = appRating;
        }    
     
        /**
         * Create an app with the given name, author, and price that is not rated
         * @param appName the app name
         * @param appAuthor the app author
         * @param appPrice the price of the app (0 if the app is free)
         */
        public App(String appName, String appAuthor, double appPrice)
        {
            name = appName;
            author = appAuthor;
            price = appPrice;        
        }
     
        /**
         * @return the app name
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * @return the app author
         */
        public String getAuthor()
        {
            return author;
        }
     
        /**
         * @return the app price
         */
        public double getPrice()
        {
            return price;
        }
     
        /**
         * Set the price of this app to the value given
         * @param newPrice new price for this app
         */
        public void setPrice(double newPrice)
        {
            if (newPrice >= 0) {
                price = newPrice;
            }
            else {
                System.out.println("Error: Price must be greater than or equal to 0");
            }
        }
     
        /**
         * @return true if this app is free, false otherwise
         */
        public boolean isFree()
        {   
            if(price == 0) {
                return true;
            }
            else {
                return false;
            }
        }
     
        /**
         * @return the app rating
         */
        public int getRating()
        {
            return rating;
        }
     
        /**
         * Set the rating of this app to the value given
         * @param newRating new rating for this app
         * Valid rating is 1-4
         */
        public void setRating(int newRating)
        {
            if((newRating >= 1) && (newRating <= 4)) {
                rating = newRating;
            }
            else if((newRating == 0) || (newRating > 4)) {
                System.out.println("Error: Valid rating range is 1-4.");
            }
        }
     
        /**
         * Reset the rating of this app to not rated
         */
        public void resetRating()
        {
            rating = 0;
        }
     
        /**
         * Increase the rating of this app by 1
         */
        public void increaseRating()
        {
            if(rating >= 0 && rating <= 3) {
                rating = rating + 1;
            }
            else if(rating == 0 || rating >= 4) {
                rating = rating;
            }
        }
     
        /**
         * Decrease the rating of this app by 1
         */
        public void decreaseRating()
        {
            if(rating == 1) {
                rating = 1;
            }
            else if(rating >= 2 && rating <= 4){
                rating = rating - 1;
            }
            else if(rating == 0){
                 rating = rating;
            }
        }
     
        /**
         * Print information on this app
         */
        public void printAppInfo()
        {
            System.out.println("---------------------------------");
            System.out.println("App: " + name);
            System.out.println("Author: " + author);
            if(price == 0) {
                System.out.println("Price: FREE");
            }
            else{
            System.out.println("Price: $" + price);
            }
            if(rating == 0) {
                System.out.println("Rating: (not rated)");
            }
            else{
            System.out.println("Rating: " + rating);
            }
            System.out.println("---------------------------------");
        }
    }

  6. #6
    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

    nothing wrong here.
    Ok, where are the problems? I see the ending }s are in that post.
    Where is the main method for executing the code?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Here's the continuation. Create an App Store, the main issue is that it requires me to do pretty much everything in data type, element type, variable type, everything it seems like in String type. When I try searching for a String using a String I can't get them to match. Or in java speak .equals(). All of the methods that are incomplete or blank are the ones that I am stuck on. They all share a common think, the String requirement. I'm going to copy and paste one method at a time to make sure I get it right.

    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     * An app store containing apps
     * 
     * Modifications:
     * CT: Create AppStore with a list of apps and basic methods to add, clear, and print apps
     * TS: Added new apps to the store inventory
     /*
    public class AppStore
    {
        private String appStoreName;
        private ArrayList<App> appList;
     
        /**
         * Create an app store with the given name
         * @param name the name of this app store
         */
        public AppStore(String name)
        {
            appStoreName = name;
            appList = new ArrayList<App>();
        }
     
        /**
         * Populate the store with a few apps.
         * Use this method to make testing easier. After creating an AppStore,
         * call this method to populate the apps, and then test your methods.
         */
        public void populateApps()
        {
            addApp("Pandora Music", "Pandora", 0);
            addApp(new App("Minecraft", "Mojang", 6.99, 3));
            addApp(new App("Coinbase", "Coinbase Global Inc.", 0, 0)); 
            addApp(new App("Telegram Messenger", "Telegram FZ LLC", .99, 0));
            addApp(new App("IPTV Extreme", "Paolo Turatti", 1.99, 0));
     
        }
     
        /**
         * Add the given app to the app store
         * @param anApp an app to add
         */
        public void addApp(App anApp)
        {
            appList.add(anApp);
        }
     
        /**
         * Create an app with the given name, author, and price and add it to the store.
         * The app starts out unrated.
         * @param name name of the app
         * @param author the app author
         * @param price the price of the app
         */
        public void addApp(String name, String author, double price)
        {
            appList.add(new App(name, author, price));
        }
     
        /**
         * @return the number of apps in the store
         */
        public int getNumberOfApps()
        {
            return appList.size();
        }
     
        /**
         * Removes all the apps from the store
         */
        public void clearAppStore()
        {
            appList.clear();
        }
     
        /**
         * Print all the apps in the store
         */
        public void printAppList()
        {
            System.out.println("============= " + appStoreName + " =============");
            if (appList.size() == 0) {
                System.out.println("No apps in the store");
            }
            else {
                for (App currentApp : appList) {
                    currentApp.printAppInfo();
                }
            }
            System.out.println("===========================================");
        }
     
        /**
         * Find an app by name
         * @param the name of the app being looked for
         * @return the name of the app if found
         */
        public App findApp(String name)
        {
            populateApps();
            String name1 = new name;
            String name2 = appList.getAppName();
            int diff = name1.compareTo(name2);      //since I couldn't get any other String comparisons to work I tried this
            for(App app : appList) {                            I know it's not correct, but I wanted to try everything.
                if(diff <= 0) {
                    System.out.println("App was found.");
                }
                return null;
            }
     
        }
    /**
         * @remove an app from the App store
         * @param is name of app to remove
         */
        public void removeApp(String name)
        {
               populateApps();
               App removeApp = null;
               for(App app : appList) {
               if(string1 > string2 || string1 < string2)) {
                  apps.remove(app);
                  removeApp = app;
                  break;
                }
              }
              return removeApp;
            }
        /**
         * @locate an app by the author name
         *  Print a list of all apps by author name
         *  If there is no app by author name return empty list (not null)
         */
        public App getAppsByAuthor(String name)
        {
            populateApps();
            ArrayList<App> author = new ArrayList<App>();
            for(App app : appList) {
                String appAuthor = app.getAuthor();
                if(app.getAuthor().equals(null)){
                     return null;
                }
                return author;
            } 
        } 
        /**
         *@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 void getNumAppsWithRating(int rating)
        {
     
        }
        /**
         * @prints a summary of the app store
         *  prints total number of apps, name of store,
         *  number of apps with each rating and unrated apps
         */
        public void printAppStoreSummaryStats()
        {
     
        }
    }

  8. #8
    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

    the methods that are incomplete or blank are the ones that I am stuck on
    Ok, make a list of those methods that need work.
    Pick one.
    Describe what that method is supposed to do and what data is available for the method to work on.
    Describe what steps the method should take to do its task.
    Write the code to implement those steps.
    Ask some specific questions about the steps that you are having problems with.

    When that method is working, move to the next method in the list and repeat the above for that method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Yep, on it. Here we go.

    //TODO: ------------------------ 2 --------------------------
    // TODO: Uncomment the method wrapper and implement according to the header comment.
    // TODO: Do not change the method signature that is given.
    // TODO: Hint: One approach is to use a for-each to loop over each app in the list.
    // TODO: Inside the loop check if the app name matches and if so, return the app.
    // TODO: If the loop completes without returning, the app is not in the list and
    // TODO: you can return null.
    //

    /** Starting at top and working my way down. Skip documentation. It
    * Find an app by name says find an app by searching its name. If the name matches, return
    * @param the name of the app being looked for that app. If the loop completes with no match, return null. Parameter
    * @return the name of the app if found should be String type && the name of the app we're looking for. Can't
    */ change the signature so to start I'll use the hint of use a for-each loop.
    public App findApp(String name) ElementType element : collection for(App app : appList) {
    { now I get confused. I want to compare the String I'm going to input
    populateApps(); <--- I wrote this because someone said vs. the String in the collection. Do I write string1.equals(string2) { ? or
    that would call that method and get do I create a variable? call the method that stores them? I thought I
    int index = 0; the values. could compare them by length so I tried int len = nums.length() but
    for(App app : appList) { don't know how to compare it to the stored String. I guess this is when
    I need direction. Is this the method appList.getPopulateApps() I need
    if(diff <= 0) { to call?
    System.out.println("App was found.");
    }
    return null;
    }

    }
    Last edited by misterCrypto; August 6th, 2021 at 12:34 AM.

  10. #10
    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

    Sorry, I can't find the items I was asking for in all that text. A lot of that text looks like code and not like English sentences. Leave off writing code until you have the list in English of steps the method is supposed to do.
    What is the name of the method you are working on?
    What is that method supposed to do?
    What data does the method have to work with?

    If there is any code in a post, be sure to wrap that code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Method #1

    public App findApp(String name)
    {

    }

    //TODO: ------------------------ 2 --------------------------
    // TODO: Uncomment the method wrapper and implement according to the header comment.
    // TODO: Do not change the method signature that is given.
    // TODO: Hint: One approach is to use a for-each to loop over each app in the list.
    // TODO: Inside the loop check if the app name matches and if so, return the app.
    // TODO: If the loop completes without returning, the app is not in the list and
    // TODO: you can return null.
    //

    From the TODO list information provided, I am supposed to create a method (using the signature provided above), that could possibly use a for-each loop, that searches the collection for a specific name of an app. Inside the loop I need to check if the name of the app that I'm searching for is the same name as an app in the collection. If a match is found, I am to return the name of the app. If no match is found I am to return null. The instructions say that I should delete the header comments. This method has a parameter String type which could also be the name of the app we're trying to find.

    Hopefully this is better.

    To start this method I will first look at the signature. I guess the data type is App, which I'm not familiar with, but I assume that it has to do with the ArrayList package. The method is telling us to findApp with a parameter of String type (which could also be the name of the app we're trying to find).
    Question: Should we assume that the String type parameter of the findApp method is also going to be the name of the app we’re looking for? When I see a parameter of String name should that tell me something more than I’m seeing on the surface?
    What we know from the information we already have is: we're conducting a search of this collection for a specific name of an app. This collection is an app store so we know that other methods are storing data.
    Obviously in the instructions they make a suggestion of using a for-each loop to execute this method, so that’s what I intend ro do.
    public App findApp(String name)
    {
    	for(App app : appList) {
                  }
     }

    This is pretty much where I get stuck at. I know we need to compare the String of the find App name to the String of the app names in the collection. Hopefully I answered better this time.
    Last edited by misterCrypto; August 6th, 2021 at 04:37 AM.

  12. #12
    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 better.
    Work on this step:
    Inside the loop I need to check if the name of the app that I'm searching for is the same name as an app in the collection
    I am assuming you understand what that for statement does: it gets the items in the list one at a time.
    Does the app object have a method to get its name? Call that method and get the app's name.
    Then compare the app's name to the name that was passed to the findApp method using the equals method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Yes, I understand what the for loop does, I even understand what I'm supposed to do with the list. What I don't understand is how to write it. In previous assignments and projects set/get methods were very straight forward. In this project, there is no "get" method, I think I'm supposed to call the
    populateApps method because it's where the Apps are stored. Here is the code to that method:

     
       public void populateApps()
        {
            addApp("Pandora Music", "Pandora", 0);
            addApp(new App("Minecraft", "Mojang", 6.99, 3));
            addApp(new App("Coinbase", "Coinbase Global Inc.", 0, 0)); 
            addApp(new App("Telegram Messenger", "Telegram FZ LLC", .99, 0));
            addApp(new App("IPTV Extreme", "Paolo Turatti", 1.99, 0));
     
        }

    So then would the code look like this?
     
    public App findApp(String name)
    {
            for(App app : appList) {
               populateApps();
     
             }
    }
    Am I even close to right track? It's hard to know when you're moving in the right direction, especially with the naming of calls and expressions. I'm not sure how to check the name of the populateApps() method. I know what needs to be done, but I don't how to tell the compiler what to do. Frustrating.

  14. #14
    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

    would the code look like this?
    No. The call to populateApps(); should NOT be done anywhere inside of the findApp method.
    The instructions say:
    After creating an AppStore, call this method to populate the apps, and then test your methods.
    That says to call the populate method ONE time after the AppStore instance was created.
    After the call to the populate method, then call the methods to be tested.

    what I'm supposed to do with the list
    ...
    don't understand is how to write it.
    I listed the steps of what to do inside if the loop in post#12
    get the app's name. - Use a get method
    Then compare the app's name to the name that was passed to the findApp method using the equals method.
    The comments in post#11 say what to do:
    // TODO: Inside the loop check if the app name matches and if so, return the app.
    // TODO: If the loop completes without returning, the app is not in the list and
    // TODO: you can return null.
    Do you know that the foreach version of the for loop retrieves the next object from the list?
    Given an instance of a class, do you know how to call one of its methods?
    See the getAppsByAuthor method for an example.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    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 were you able to use loops to go through lists and call methods in the code in this thread:
    https://www.javaprogrammingforums.co...ach-loops.html
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    I was able to work through the other thread because that was our first encounter with ArrayLists and there were more examples to use for help. Even with more examples though, I was still asking questions in this forum. I'm trying to use my getAppsByAuthor method as an example but I can't even get that method to compile.

    public App findApp(String name)
    {
        for(App app : appList) {
            String name = app.getName();
        if(app.getName().equals(null)) {
                return null;
          }
        else {
                return app.getName();
             }
        }
    }

    What's wrong here?

  17. #17
    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(app.getName().equals(null)) {
    Why compare the contents of the app's name with null? Isn't the objective to find an app with a name equal to the value passed in the name variable? Compare against name not null.

    Go back and read what the method is supposed to do. See post#14

    I can't even get that method to compile.
    When you get errors, you need to copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    I understand, completely. When I tried

    if(app.getName().equals(name)) {

    I couldn't get the method to return the name. I keep getting error messages of incompatible types, can't convert String to App. When I compared to null I was able to at least return null, and then I thought I could work off of that. I will start transcribing the error messages, my apologies.

  19. #19
    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

    I keep getting error messages of incompatible types
    When you get errors, you need to copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    It says Incompatible Types : String cannot be converted to App.

  21. #21
    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

    What source line is that error message on?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Maybe it has to do with BlueJ and BlueJ doesn't give line numbers. So, everything above that method, nothing has changed.

    public App findApp(String name)
    {
        for(App app : appList) {
            String appName = app.getName();
            if(app.getName().equals(name)) {
                return name;                                        <---- Error Message
            }
            else{
     
                  }
           }
    }

  23. #23
    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

    public App findApp(String name) <<<< Returns an App NOT A STRING
    ...
    return name; <<< NOT an App; Change to return the App
    Change the return statement to return an App value NOT a String

    What is this statement for?
    String appName = app.getName();
    appName is not used anywhere.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: ArrayList<> String searches and loops

    Ok, I did String name =... because earlier you referred me to my getAppsByAuthor method and I did something similar. I thought that was correct so I tried to use it again, only changing the names. I was wrong. I tested this method and it worked. On to the next one. Thank you for the direction, I think I learned a lot from this method. We shall see.

  25. #25
    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, good luck with the next method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    misterCrypto (August 9th, 2021)

Page 1 of 3 123 LastLast

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