Results 1 to 11 of 11
  1. #1
    CMXVII is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default New To Java: Trying to loop arraylist to return specified values

    Not sure what I am missing here, but I'm not getting any error messages. The output currently is "[]". The intended output is the movie titles that correspond with the user input category......if user input Scifi, the output should be "Star Wars" and "2001: A Space Odyssey". Apologies in advance, I'm a total newb to Java. Current code is as follows:


    MAIN Class

    import java.util.Scanner;

    public class ListMoviesApp {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    System.out.println("Welcome to the Movie List App!");
    System.out.println("There are 100 movies in the list");

    String title = null;
    String category = null;

    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

    System.out.println("What genre are you interested in?");
    Movie newMovie = new Movie(title,category);
    category = sc.nextLine().toLowerCase();
    newMovie.setCategory(category);

    System.out.println(MovieDB.getMovies(category));

    System.out.println("Would you like to continue? (y/n)");
    choice = sc.nextLine();
    }

    System.out.println("Thanks for using the Movies List App! Goodbye!");
    sc.close();
    }
    }

    MOvie Class

    public class Movie {

    private String title;
    private String category;

    public Movie(String title, String category) {
    this.title = title;
    this.category = category;
    }

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getCategory() {
    return category;
    }

    public void setCategory(String category) {
    this.category = category;
    }
    }

    MovieDB Class

    import java.util.ArrayList;

    public class MovieDB {

    private static ArrayList<Movie> allMovies = new ArrayList<>(12);

    public static ArrayList<Movie> getAllMovies() {
    allMovies.add(new Movie("Citizen Kane", "drama"));
    allMovies.add(new Movie("Casablanca", "drama"));
    allMovies.add(new Movie("The Wizard Of Oz", "musical"));
    allMovies.add(new Movie("Singin' In The Rain", "musical"));
    allMovies.add(new Movie("Star Wars", "scifi"));
    allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
    allMovies.add(new Movie("Psycho", "horror"));
    allMovies.add(new Movie("King Kong", "horror"));
    allMovies.add(new Movie("Annie Hall", "comedy"));
    allMovies.add(new Movie("M*A*S*H", "comedy"));
    allMovies.add(new Movie("Lion King", "animated"));
    allMovies.add(new Movie("Fantasia", "animated"));

    return allMovies;
    }

    public static ArrayList<Movie> getMovies(String category) {
    if (allMovies.isEmpty()) {
    getAllMovies();
    }

    ArrayList<Movie> movies = new ArrayList<>();
    // Here I need code to return only movies from a specific category
    // From input from the user in the main
    if (category == null) {
    return movies;
    }

    for (Movie movie : allMovies) {
    if (movie.getCategory().equals(category)) {
    movies.add(movie);
    }
    }

    return movies;
    }
    }
    Last edited by CMXVII; 03-20-2017 at 06:51 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: New To Java: Trying to loop arraylist to return specified values

    Your program works fine. All you need to do is to loop over the result of the call to MovieDB.getMovies(String). You cannot put an ArrayList in System.out.println (Well, you can, but the output is garbage). So, iterate over the result and print the titles individually.

    Another observation: your newMovie variable does nothing and is not needed. You can remove it.

    Another observation: you are using equals() to compare the category to see if it needs to be in the result. That is perfectly fine, but you should keep in mind that "Scifi" does not equals "scifi". If that's not what you want, you should look into using equalsIgnoreCase()

    Also, put your code in [code][/code] tags, so it keeps indenting and formatting.
    Last edited by SurfMan; 03-20-2017 at 10:58 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    CMXVII is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Re: New To Java: Trying to loop arraylist to return specified values

    Quote Originally Posted by CMXVII View Post
    Not sure what I am missing here, but I'm not getting any error messages. The output currently is "[]". The intended output is the movie titles that correspond with the user input category......if user input Scifi, the output should be "Star Wars" and "2001: A Space Odyssey". Apologies in advance, I'm a total newb to Java.

    UPDATE: Ok, so I used an override toString in the Movie class (added below). Now Im getting the right content, but the output formatting is not what I expected:

    output for scifi:
    [Star Wars, 2001: A Space Odyssey]
    desired output for scifi:
    Star Wars
    2001: A Space Odyssey


    MAIN Class
    Java Code:
    import java.util.Scanner;
    
    public class ListMoviesApp {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Welcome to the Movie List App!");
    		System.out.println("There are 100 movies in the list");
    		
    		String title = null;
    		String category = null;
    		
    		String choice = "y";
    	    while (choice.equalsIgnoreCase("y")) {
    		
    		System.out.println("What genre are you interested in?");
    		Movie newMovie = new Movie(title,category);
    		category = sc.nextLine().toLowerCase();
    		newMovie.setCategory(category);
    		
    		System.out.println(MovieDB.getMovies(category).toString());
    		
    	    System.out.println("Would you like to continue? (y/n)");
    	    choice = sc.nextLine();
    	}
    	    
    	System.out.println("Thanks for using the Movies List App! Goodbye!");
        sc.close();
    	}
    }
    MOvie Class
    Java Code:
    public class Movie {
    
        private String title;
        private String category;
    
        public Movie(String title, String category) {
            this.title = title;
            this.category = category;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getCategory() {
            return category;
        }
    
        public void setCategory(String category) {
            this.category = category;
        }
    
        public String toString(){
            return title;
        }
    }
    MovieDB Class
    Java Code:
    import java.util.ArrayList;
    
    public class MovieDB {
    
    private static ArrayList<Movie> allMovies = new ArrayList<>(12);
    
    public static ArrayList<Movie> getAllMovies() {
        allMovies.add(new Movie("Citizen Kane", "drama"));
        allMovies.add(new Movie("Casablanca", "drama"));
        allMovies.add(new Movie("The Wizard Of Oz", "musical"));
        allMovies.add(new Movie("Singin' In The Rain", "musical"));
        allMovies.add(new Movie("Star Wars", "scifi"));
        allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
        allMovies.add(new Movie("Psycho", "horror"));
        allMovies.add(new Movie("King Kong", "horror"));
        allMovies.add(new Movie("Annie Hall", "comedy"));
        allMovies.add(new Movie("M*A*S*H", "comedy"));
        allMovies.add(new Movie("Lion King", "animated"));
        allMovies.add(new Movie("Fantasia", "animated"));
    
        return allMovies;
    }
    
    public static ArrayList<Movie> getMovies(String category) {
        if (allMovies.isEmpty()) {
            getAllMovies();
        }
    
        ArrayList<Movie> movies = new ArrayList<>();
        // Here I need code to return only movies from a specific category
        // From input from the user in the main
        if (category == null) {
            return movies;
        }
    
        for (Movie movie : allMovies) {
            if (movie.getCategory().equals(category)) {
                movies.add(movie);
            }
        }
    
        return movies;
    }
    }
    adding 4 characters to save
    Last edited by CMXVII; 03-20-2017 at 03:19 PM.

  4. #4
    CMXVII is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Re: New To Java: Trying to loop arraylist to return specified values

    I update my post. Now Im trying to figure out how to format the output.

    Couple of questions about observations:

    I used the newMovie object to be able to save the user input and set it. I assume I do need it.

    regarding the equals(), I initially set the user input toLowerCase so that using the equals() would match the DB formatting. Is using equalsIgnoreCase() better than setting the user input toLowerCase()?

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: New To Java: Trying to loop arraylist to return specified values

    The output should be done inside a loop. For each movie from the collection, print whatever you get from getTitle(). Use the same for-construction that you did for finding by category on line 36-40.

    Your code does not save the Movie right now. So at this point the variable is useless. Maybe you will add that functionality later?

    equals() is not better or worse than equalsIgnoreCase(). It depends on what your requirements are. If you do this: input.toLowerCase().equals(category.toLowerCase()) , then you might as well use equalsIgnoreCase().
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    CMXVII is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Re: New To Java: Trying to loop arraylist to return specified values

    I researched and found this alternative way of altering the output of the ArrayList:
    Java Code:
    System.out.println(MovieDB.getMovies(category).toString().replaceAll("^.|.$", "").replace(", ", "\n"));
    Thank for all your input. Appreciate it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: New To Java: Trying to loop arraylist to return specified values

    That seems like a pointlessly complex way of formatting.
    What's wrong with just looping over the array and doing println(<the film name>) for each entry?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: New To Java: Trying to loop arraylist to return specified values

    Quote Originally Posted by CMXVII View Post
    I researched and found this alternative way of altering the output of the ArrayList:
    Java Code:
    System.out.println(MovieDB.getMovies(category).toString().replaceAll("^.|.$", "").replace(", ", "\n"));
    Thank for all your input. Appreciate it.


    Stop. Now. That is not how you do it. Like Tolls says, take the 3 seconds to use the title. With Java 8 it's even simpler:
    Java Code:
    MovieDB.getMovies(category).stream().map(Movie::getTitle).forEach(System.out::println);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    CMXVII is offline Member
    Join Date
    Mar 2017
    Posts
    5
    Rep Power
    0

    Default Re: New To Java: Trying to loop arraylist to return specified values

    Sorry for the late response. I apologize, I wasnt trying to ruffle any tail feathers. I just found a different way to achieve what I was looking for. I do appreciate you taking the time to show me the more appropriate way. I will research into the methods you provided to understand more about it.

    Thank you again.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: New To Java: Trying to loop arraylist to return specified values

    OK.
    What I will leave you with is this.
    Quote Originally Posted by Jamie Zawinski
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    Outside of obvious pattern matching areas a regex is rarely the best answer (the above does not mean, as is sometimes mistaken, "do not use regexes").

    The reason in this case is not the complexity of the regex (which is the usual reason for the above quote), but that you are already looping over the array to build up the String (this is done in the toString() call) that you then regex out the commas for carriage returns (or whatever). In other words, it's unnecessary make-work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: New To Java: Trying to loop arraylist to return specified values

    Besides that, a toString() on an ArrayList is a bad idea. Always. In my 20 years of Java programming, I have never ever felt the need to do a toString() on any kind of List.

    If you want to go the toString() route, by all means make sure you have a proper toString() on the Movie class.

    I see toString() more as a debugging feature than anything else. I will not rely on it for day to day use.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 03:15 PM
  2. How to return all values from a for loop?
    By ilovemysillybanana in forum New To Java
    Replies: 1
    Last Post: 04-12-2013, 05:21 PM
  3. Return ArrayList function - Java
    By skp123 in forum New To Java
    Replies: 23
    Last Post: 05-01-2011, 05:11 AM
  4. Return values from a while loop
    By gaborn415 in forum New To Java
    Replies: 7
    Last Post: 04-11-2011, 06:12 AM
  5. java method with multiple return values
    By Ms.Ranjan in forum New To Java
    Replies: 1
    Last Post: 06-18-2009, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •