ArrayList and Iteration of a String

Hello, i am having a problem with iteration. I have created a rentVideo method and an arraylist called currentRentVideo. My problem seems to be in line 80. i am getting an unusual error message. it is:

Exception in thread "main" java.lang.ClassCastException: edu.asu.east.labexample.Rental

at edu.asu.east.labexample.Member.toString(Member.java:80)

at java.lang.String.valueOf(Unknown Source)

at java.io.PrintStream.print(Unknown Source)

at java.io.PrintStream.println(Unknown Source)

at edu.asu.east.labexample.VideoTest.main(VideoTest.java:66)

Can anyone spot what i am going wrong. how would you go about correcting this. It looks line to me. Here is my code:

package edu.asu.east.labexample;

import java.util.*;

publicclass Member{

private String memberName;

private String memberAddress;

private String memberPhone;

private String memberStatus;

List currentRentVideo =new ArrayList();

@SuppressWarnings("unchecked")

publicvoid rentVideo(Video video){

Rental rental =new Rental(video);

currentRentVideo.add(rental);

}

public List getcurrentRentVideo(){

return currentRentVideo;

}

publicvoid setcurrentRentVideo(List rentVideo){

this.currentRentVideo = rentVideo;

}

public String getMemberAddress(){

return memberAddress;

}

publicvoid setMemberAddress(String memberAddress){

this.memberAddress = memberAddress;

}

public String getMemberName(){

return memberName;

}

publicvoid setMemberName(String memberName){

this.memberName = memberName;

}

public String getMemberPhone(){

return memberPhone;

}

publicvoid setMemberPhone(String memberPhone){

this.memberPhone = memberPhone;

}

public String getMemberStatus(){

return memberStatus;

}

publicvoid setMemberStatus(String memberStatus){

this.memberStatus = memberStatus;

}

public String toString(){

Iterator iter = currentRentVideo.iterator();

String List ="";

while (iter.hasNext()){

List = (String) iter.next();

}

return getMemberName() +" " + getMemberAddress() +" "

+ getMemberPhone() +" " + getMemberStatus()

+ getcurrentRentVideo() + List;

}

}

Here is my Video class:

package edu.asu.east.labexample;

import java.util.*;

publicclass Video{

private String videoTitle;

private String videoRating;

private String videoCategory;

private Date videoReleaseDate;

private String videoDirector;

public String getVideoCategory(){

return videoCategory;

}

publicvoid setVideoCategory(String videoCategory){

this.videoCategory = videoCategory;

}

public String getVideoDirector(){

return videoDirector;

}

publicvoid setVideoDirector(String videoDirector){

this.videoDirector = videoDirector;

}

public String getVideoRating(){

return videoRating;

}

publicvoid setVideoRating(String videoRating){

this.videoRating = videoRating;

}

public Date getVideoReleaseDate(){

return videoReleaseDate;

}

publicvoid setVideoReleaseDate(Date videoReleaseDate){

this.videoReleaseDate = videoReleaseDate;

}

public String getVideoTitle(){

return videoTitle;

}

publicvoid setVideoTitle(String videoTitle){

this.videoTitle = videoTitle;

}

public String toString(){

return getVideoTitle() +" " + getVideoDirector() +" "

+ getVideoRating() +" " + getVideoCategory() +" "

+ getVideoReleaseDate();

}

}

[code]

Here is my VideoTest main:

[code]

package edu.asu.east.labexample;

import java.util.*;

publicclass VideoTest{

@SuppressWarnings("deprecation")

publicstaticvoid main(String[] args){

Video video1 =new Video();

video1.setVideoTitle("Ghost World");

video1.setVideoDirector("Terry Zwigoff");

video1.setVideoRating("R");

Date dateVideo1 =new Date(101, 6, 20);

video1.setVideoReleaseDate(dateVideo1);

video1.setVideoCategory("Comedy");

Video video2 =new Video();

video2.setVideoTitle("Batman Begins");

video2.setVideoDirector("Christopher Nolan");

video2.setVideoRating("PG-13");

Date dateVideo2 =new Date(105, 6, 1);

video2.setVideoReleaseDate(dateVideo2);

video2.setVideoCategory("Action");

Video video3 =new Video();

video3.setVideoTitle("Oldboy");

video3.setVideoDirector("Chan-Wook Park");

video3.setVideoRating("R");

Date dateVideo3 =new Date(103, 0, 1);

video3.setVideoReleaseDate(dateVideo3);

video3.setVideoCategory("Action");

Member member1 =new Member();

member1.setMemberName("Tedd C. Griswold");

member1.setMemberPhone("555-555-5555");

member1.setMemberAddress("1313 Mockingbird Lane");

member1.setMemberStatus("Active");

Member member2 =new Member();

member2.setMemberName("Tom Waits");

member2.setMemberPhone("480-867-5309");

member2.setMemberAddress("Heartattack and Vine");

member2.setMemberStatus("Active");

Member member3 =new Member();

member3.setMemberName("Harvey Pekar");

member3.setMemberPhone("123-456-7890");

member3.setMemberAddress("555 N Federal St.");

member3.setMemberStatus("Active");

member3.rentVideo(video3);

System.out.println(member1);

System.out.println(member2);

System.out.println(member3);

}

}

Message was edited by:

rexdart

[10822 byte] By [rexdarta] at [2007-11-26 19:34:07]
# 1

You cast the object you get from currentRentVideo to a String.

But currentRentVideo contains (at least) Rental objects.

By the way, that toString method loops through the iterator, but only keeps the last item it finds. You could more efficiently just grab the last item on the list with get().

paulcwa at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...
# 2
I am not sure i follow you. could you give me an example?
rexdarta at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...
# 3
Can anyone help? My book lacks enough examples and I learn best from seeing it in the correct context.Message was edited by: rexdart
rexdarta at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...
# 4

What part didn't you get?

Look at your own code. Here's where you declare currentRentVideo:

List currentRentVideo = new ArrayList();

And here's where you add to it:

public void rentVideo(Video video) {

Rental rental = new Rental(video);

currentRentVideo.add(rental);

}

OK, now what kind of object are you adding to the list?

Now look at where the exception is happening. First:

Iterator iter = currentRentVideo.iterator();

Note: iter is iterating over the contents of currentRentVideo. Given the above, what is contained in currentRentVideo?

OK now let's look at what you're doing with the contents of the list, via the iterator:

String List = "";

while (iter.hasNext()) {

List = (String) iter.next();

}

You're casting one of the contents from currentRentVideo to a String. Why? What are the contents of currentRentVideo? What do you think will happen when you say they're Strings if they're really not?

By the way, you should follow Java naming conventions. Local variables, non-constant fields, and methods, should all be named starting with a lower-case letter. Also, it's a bad idea to give your variable the same name as a commonly-used interface (List).

Also look at that loop again. Every time you pass through that loop body? What happens? The variable "List" is assigned a value, overwriting whatever value it previously had. That means that you're throwing away the results of every previous time you passed through that list, except for the last one. You probably meant to append each value to a larger string.

But for that matter, you don't need the loop at all. Objects (including ones that implement java.util.List) will have toString() methods as well. In the last statement in your toString method:

return getMemberName() + " " + getMemberAddress() + " "

+ getMemberPhone() + " " + getMemberStatus()

+ getcurrentRentVideo() + List;

You call getcurrentRentVideo(),. which returns currentRentVideo, which is then transformed into a String (because you're appending strings together in that statement) via its own toString() method. So it's not clear what the loop in your toString() method is supposed to accomplish, since whatever it's likely meant to do is already happening in a much easier way.

By the way it's really debatable whether you should be creating getters/setters for currentRentVideo, or declaring it as package-private as you have. Why would any other class need to completely change the list of current video rentals the customer has? You're breaking encapsulation.

paulcwa at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...
# 5

i am sorry i am not understanding. Am i iterating over the wrong thing. should it be my object 'rental'? I think i understand you comment on my toString line. Could you give me an example of a correct version of iteration that i could use as a reference? I really thank you for your help.

Message was edited by:

rexdart

rexdarta at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...
# 6

How you're iterating isn't the issue. It's (1) what you're doing with the stuff that you're iterating over, and (2) the fact that you're iterating at all.

And a code sample wouldn't work since we're talking about a bug in your code. Are you trying to get me to just write your code for you?

Look: What objects are in your currentRentVideo list?

paulcwa at 2007-7-9 22:07:26 > top of Java-index,Java Essentials,New To Java...