Results 1 to 18 of 18
Thread: Need Help with ArrayList
- 12-03-2012, 04:37 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Need Help with ArrayList
Movie Class
Inventory ClassJava Code:public class Movie { String title; int yearReleased; double rating; int quantity; public Movie (String title, int year, double rating, int quantity){ this.title = title; this.yearReleased = year; this.rating = rating; this.quantity = quantity; } public Movie(){ quantity = 1; } }
I get the error:Java Code:import java.util.ArrayList; public class Inventory { ArrayList <Movie> movieList = new ArrayList <Movie>(); Movie theMovie = new Movie(); public void add(String title, int year, double rating){ movieList.add(title, year, rating); } }
"The method add(int, Movie) in the type ArrayList<Movie> is not applicable for the arguments (String, int, double)"
Am I starting this correctly at all? My objective is: Contain a method “add” that adds a movie to the database. Add takes three arguments – a title (String), year released (Integer) and rating (Double). The “add” method will add the movie in question to the ArrayList if the movie does not already exist in the database. If it does it will simply increase the quantity of the movie. Note that you can use the title and year released as “keys” to determine if a movie exists in the database (i.e. there could be many different Batman movies in the database, but there will only be one Batman released in a given year). Hint: use the “equals” method on the String class to compare if two Strings are equivalent.
why is that happening? I'm new to Java. Thanks for your help!Last edited by vx117; 12-03-2012 at 04:50 AM.
-
Re: Need Help with ArrayList
Your ArrayList is an ArrayList of Movie objects, and so its add method must accept a Movie object. You're trying to to stuff in a String, int and double, all at once and the compiler is rightfully complaining. Instead why not take that information, create a valid Movie object from it, and then add that object to the movieList ArrayList.
- 12-03-2012, 04:47 AM #3
Senior Member
- Join Date
- Nov 2012
- Posts
- 221
- Rep Power
- 1
Re: Need Help with ArrayList
possible solution for you, part of it is done, you need to think about the rest
also you may want to add a print method so you can test what you have added, and possible give the inventory a constructor where you would define objects you will use through the class, Scanners, Arraylists etc. its not the best practice to define like i have.Java Code:import java.util.ArrayList; public class Inventory { private ArrayList<Movie> movieList = new ArrayList<Movie>(); private Movie m; public void addMovie(){ Movie m=new Movie(); String title; int yearReleased; //the brief says use equals method of String, suggests you want to store this as a string not an int? double rating; int quantity; //assign value to these variable how would you do this? //possibly add a Scanner(System.in) to collect user input look this up //then: m=new Movie(title, yearReleased, rating, quantity); movieList.add(m); } }Last edited by monkeyjr97; 12-03-2012 at 04:54 AM.
-
Re: Need Help with ArrayList
I politely disagree. The original poster's addMovie method signature was good as is, and a default method with no parameters and with user interface code contained in it is tainting the Inventory class by giving it responsibilities that it shouldn't have. UI should be in a completely different location, and the addMovie method should not have any user input code whatsoever, and also adding null values won't help. It should take the parameters passed and create a Movie object with it pure and simple.
To correct this potentially misleading advice, let me suggest what I think is best:
Here you use the information passed into the method to create a Movie object and add it to the list. Nothing more and nothing less, and no user interface code where it doesn't belong. Inventory should concern itself only with the list of Movies and nothing else. It should be usable in a GUI or a console application and not tied to one or the other.Java Code:public void add(String title, int year, double rating){ movieList.add(new Movie(title, year, rating, 1)); }
- 12-03-2012, 05:07 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
Thanks a lot. However, now I don't know exactly how to start the second portion of my problem.
If it does it will simply increase the quantity of the movie. Note that you can use the title and year released as “keys” to determine if a movie exists in the database (i.e. there could be many different Batman movies in the database, but there will only be one Batman released in a given year). Hint: use the “equals” method on the String class to compare if two Strings are equivalent.
I'm not very good with Arrays, and I don't get the thing about using "keys" to increase quantity. Can someone please point to how to start it? Also, if you know a website or video that effectively teaches Arrays in a understandable fashion, please link me. thanks.
-
Re: Need Help with ArrayList
Ah, I did not see that. inside of the addMovie method, you will want to use a for loop to go through the ArrayList, looking at the Movie objects it has to see if any match the descriptions passed into the method. If it is found then you'll want to increment the quantity variable of the contained Movie object.
By the way, that's a terrible program design on the part of your instructor. A Movie object should not have to worry about its own quantity in the ArrayList.
- 12-03-2012, 02:37 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
Okay so far, I have this:
Java Code:for (int i = 0; i < movieList.size(); i++) { if (theMovie instanceof Movie){ Movie theMovie = (Movie) theMovie; if (this.title.equals(theMovie.title)) return true; } else{ movieList.add(new Movie(title, year, rating, 1)); } }
Is this how you check for the movie object and look for any matches in the description? I tried using an equals method, but I don't think I'm doing it right. Also, how do you increment a specific quantity variable, in this case, I wish to increment quantity if the two match.
-
Re: Need Help with ArrayList
I'm not sure why you are using "this".title or why you're using instanceof since the movieList only holds Movie objects and nothing else.
Again I would iterate through the movieList with a for list, and yes extract the title String and possibly the year from each Movie in the list and compare with the title and year that is being passed into the method, and if a match, then get the quantity from that Movie increase it by one and put it back into that Movie object and return from the method. Otherwise create a new Movie and add it to the list with a quantity of 1.
- 12-03-2012, 04:52 PM #9
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
So I would use something like this to get the title and year from the object Movie right?
How do I just specifically pull the String title and int year?Java Code:Movie thisMovie = movieList.get(i);
The same works with quantity right?
Also, are getters and setters in my movie object required for this to work?Last edited by vx117; 12-03-2012 at 05:01 PM.
-
Re: Need Help with ArrayList
- 12-03-2012, 08:35 PM #11
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
Movie Class
Inventory ClassJava Code:public class Movie { String title; int yearReleased; double rating; int quantity; public Movie (String title, int year, double rating, int quantity){ this.title = title; this.yearReleased = year; this.rating = rating; this.quantity = quantity; } public Movie(){ quantity = 1; } public Movie (String title, int year){ } public void setTitle(String title){ this.title = title; } public String getTitle(){ return title; } public void setYear(int year){ this.yearReleased = year; } public int getYear(){ return yearReleased; } public void setRating(double rating){ this.rating = rating; } public double getRating(){ return rating; } public void setQuantity(int quantity){ this.quantity = quantity; } public int getQuantity(){ return quantity; } }
Main MethodJava Code:import java.util.ArrayList; public class Inventory { ArrayList <Movie> movieList = new ArrayList <Movie>(); Movie theMovie = new Movie(); public void add(String title, int year, double rating){ for (int i = 0; i < movieList.size(); i++) { String thisTitle = movieList.get(i).getTitle(); int thisYear = movieList.get(i).getYear(); int thisQuantity = movieList.get(i).getQuantity(); if (thisTitle == movieList.get(i).getTitle() ) { if (thisYear == movieList.get(i).getYear()) { thisQuantity ++; } else{ movieList.add(new Movie(title, year, rating, 1)); } } else{ movieList.add(new Movie(title, year, rating, 1)); } } } public void remove(String title, int year) { for (int i = 0; i < movieList.size(); i++) { String thisTitle = movieList.get(i).getTitle(); int thisYear = movieList.get(i).getYear(); int thisQuantity = movieList.get(i).getQuantity(); if (thisTitle == movieList.get(i).getTitle() && thisQuantity > 1) { thisQuantity --; if (thisQuantity == 0){ movieList.remove(new Movie(title, year)); } } else{ movieList.remove(new Movie(title, year)); } } } public String toString(){ return movieList.toString(); } }
Java Code:public class TestInventory { public static void main(String[] args) { Inventory a = new Inventory(); a.add("Happy Gilmore", 1996, 3.5); a.add("Star Wars", 1977, 4.8); a.add("Return of the Jedi", 1983, 3.9); a.add("The Nightmare Before Christmas", 1993, 5.0); a.add("Return of the Jedi", 1983, 3.9); System.out.println(a.toString() ); a.remove("Return of the Jedi", 1983); System.out.println(a.toString() ); a.remove("The Nightmare Before Christmas", 1993); System.out.println(a.toString() ); } }
I'm only getting brackets printed out. like this: [] [] []
Can someone please look over my code? thanks.
-
Re: Need Help with ArrayList
Your add method is still not right. Problems include:
- You're comparing Strings (the title String) with == when you should be using the equals method.
- Your else blocks are in in a bad location as they look to be adding in the Movie with each iteration of the for loop if the method's fields doesn't match with the current Movie in the list. You need to in fact get rid of the else blocks and consider adding the Movie *after* the for loop ends.
- If a Movie match is found in the for loop, you need to update the quantity, as you're doing, but then use that value to *set* the quantity field of the found Movie object.
- If a Movie match is found, you should call return; right after updating quantity so as to stop any further searching and to prevent the Movie from being added more than once.
- As your code is currently written, a new Movie will only be added if the for loop actually runs. Since the ArrayList size() is 0 at the start of the program, the for loop will never run and no movies will be added to the list. This is why you're seeing no movies in the list and this will be solved if you follow my recs and add the new Movie *after* the for loop.
- Most importantly, you should be doing active debugging to try to solve why your code isn't working. You should at least sprinkle println statements throughout this or any poorly working method to see what is happening as the program runs.
- 12-04-2012, 01:26 AM #13
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
I did some changes like removing the else blocks and using the equals method, but I'm confused on these points:Java Code:public void add(String title, int year, double rating){ for (int i = 0; i < movieList.size(); i++) { String thisTitle = movieList.get(i).getTitle(); int thisYear = movieList.get(i).getYear(); int thisQuantity = movieList.get(i).getQuantity(); if (thisTitle.equals(movieList.get(i).getTitle()) ) { if (thisYear == movieList.get(i).getYear()) { thisQuantity ++; } } } movieList.add(new Movie(title, year, rating,1)); } public void remove(String title, int year) { for (int i = 0; i < movieList.size(); i++) { String thisTitle = movieList.get(i).getTitle(); int thisYear = movieList.get(i).getYear(); int thisQuantity = movieList.get(i).getQuantity(); if (thisTitle.equals(movieList.get(i).getTitle()) && thisQuantity > 1) { thisQuantity --; if (thisQuantity == 0){ movieList.remove(new Movie(title, year)); } } else{ movieList.remove(new Movie(title, year)); } } movieList.remove(new Movie(title, year)); } public String toString(){ movieList.toString(); return movieList.toString(); } }
If a Movie match is found in the for loop, you need to update the quantity, as you're doing, but then use that value to *set* the quantity field of the found Movie object.How exactly would I do those? Do I need to overload my constructors or is there a simpler way?If a Movie match is found, you should call return; right after updating quantity so as to stop any further searching and to prevent the Movie from being added more than once.
Also is my toString() function incorrect? This is my console output:
[Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]
[Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]
[Movie@6876fb1b, Movie@5b5fdf31, Movie@733638d4, Movie@6ccd2163, Movie@f4b2263]
Thanks again for putting up with a complete noob :)
-
Re: Need Help with ArrayList
You need to give the Movie class a decent toString() method.
Also, you're not doing my points 3 and 4 -- you're not calling setQuantity on the found Movie, and you're not calling return if the movie has been found.
- 12-04-2012, 02:30 AM #15
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
I made the changes, it this what you meant?
Also, what do you mean by a decent toString() method?Java Code:public void add(String title, int year, double rating){ for (int i = 0; i < movieList.size(); i++) { String thisTitle = movieList.get(i).getTitle(); int thisYear = movieList.get(i).getYear(); int thisQuantity = movieList.get(i).getQuantity(); if (thisTitle.equals(movieList.get(i).getTitle()) ) { if (thisYear == movieList.get(i).getYear()) { thisQuantity ++; theMovie.setQuantity(thisQuantity); return; } } } movieList.add(new Movie(title, year, rating,1)); }
- 12-04-2012, 02:30 AM #16
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Need Help with ArrayList
You get that kind of output from the toString() method because your Movie class doesn't override the toString() method, so it is using the one defined in the java.lang.Object class. Return the string representation of the Movie class using the information you want to show. For examples:
You will be likely use more fields to create the toString(). In this case you can use the StringBuilder class for string concatenation.Java Code:class Movie { String title; public String toString() { return "title=" + title; } }Website: Learn Java by Examples
- 12-04-2012, 05:13 AM #17
Member
- Join Date
- Dec 2012
- Posts
- 19
- Rep Power
- 0
Re: Need Help with ArrayList
But is there another way to do this without going back into the Movie class and create a toString() method directly in the Inventory class?
Can you provide an example in how I can use the StringBuilder class?
- 12-04-2012, 05:32 AM #18
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Need Help with ArrayList
The toString() method that give you the information of the Movie object is best placed in the Movie class. But if you want to implement in the Inventory class you can't just return movieList.toString(). You have to iterate to movieList to get each Movie object from it, read the properties of this object to create the toString() information.
Here is an example of StringBuilder.
Java Code:public String toString() { StringBuilder sb = new StringBuilder(); return sb.append("title=").append(title) .append("; yearReleased=").append(yearReleased).toString(); }Last edited by wsaryada; 12-04-2012 at 05:42 AM.
Website: Learn Java by Examples
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
Let ArrayList point to another arraylist but with a filter.
By gonzalioz in forum Advanced JavaReplies: 1Last Post: 05-15-2011, 06:07 PM -
sorting arraylist based on another arraylist
By busdude in forum New To JavaReplies: 4Last Post: 02-07-2011, 11:48 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks