|
Subject:
|
can this be done
|
|
Posted By:
|
simwiz
|
Post Date:
|
8/23/2006 8:50:35 AM
|
Basic piece of code validating a user where the canwatchmovie method returns true or false.
if ((objectname.canWatchMovie(rating,objectname.age)) == true) { System.out.println("can watch movie"); }else {System.out.println("cannot watch movie");}
The problem is i would like to loop through a set of objectnames for example person1.canwatchmovie, person2.canwatchmovie and so forth.
i used an array list like the below where the if statement would then use a iterator and iterate through each object for the above if statement. This didnt work, any other ideas ?
ArrayList humanCanWatch = new ArrayList();
Summary. can a object name be used within if statements where their properties and methods are called?
thanks.
|
|
Reply By:
|
panacea
|
Reply Date:
|
8/23/2006 8:32:12 PM
|
What's wrong with this?
// Assumg objectname instanceof Person ArrayList humanCanWatch = new ArrayList(); // ArrayList of Person Iterator i = humanCanWatch.iterator(); while (i.hasNext()) { Person p = (Person)i.next(); if (p.canWatchMovie(rating, p.age)) System.out.println("can watch movie"); else System.out.println("cannot watch movie"); }
Seems like it would work perfectly -- are you sure you were casting i.next()? What was the error you were getting?
Jon Emerson http://www.jonemerson.net/
|
|
Reply By:
|
simwiz
|
Reply Date:
|
8/24/2006 8:14:30 AM
|
hi, thanks for the reply ! Like you said i think it was a basis of not casting! Here is the code i used and the error.
Iterator iter = canwatch.iterator(); while ( iter.hasNext() ) { if ((iter.next().canWatchMovie(rating,iter.next().age)) == true) { System.out.println("can watch movie"); }else {System.out.println("cannot watch movie");}
the error:
cannot find symbol symbol : variable age location: class java.lang.Object if ((iter.next().canWatchMovie(rating,iter.next().age)) == true)
so im guessing the compiler was expecting a separate local variable age maybe and not implementing the technique i was after.
|
|
Reply By:
|
simwiz
|
Reply Date:
|
8/24/2006 8:21:06 AM
|
I tried implementing your code and i received a compile error.
code: ArrayList humanCanWatch = new ArrayList();
Iterator i = humanCanWatch.iterator(); while (i.hasNext()) { Person p = (Person)i.next(); if (p.canWatchMovie(rating, p.age)) System.out.println("can watch movie"); else System.out.println("cannot watch movie"); }
error: main error i keep getting !!!
cannot find symbol symbol : variable age location: class java.util.ArrayList<java.lang.Object> if (p.canWatchMovie(rating, p.age)) ^
thanks inadvance
|
|
Reply By:
|
simwiz
|
Reply Date:
|
8/30/2006 8:52:24 AM
|
anyone ?
|
|
Reply By:
|
zqiao
|
Reply Date:
|
8/30/2006 10:25:40 PM
|
did you declare age? why dont rewrite your canWatchMovie method to pass one argument only (rating)? For the age itself, it will refer to the respective property of the current Person object. Besides giving you another perspective to look at, perhaps, it promotes (slightly) better programming structure.
ZQ
|
|
Reply By:
|
hotleave
|
Reply Date:
|
9/2/2006 8:33:08 PM
|
declare a array like this: person[] personArray=new person[4]; for(int i=0;i<4;i++){ if(personArray[i].canWatchMovie(rating, p.age)) System.out.println("can watch movie"); else System.out.println("cannot watch movie"); }
|