Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2006, 08:50 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default can this be done

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.
 
Old August 23rd, 2006, 08:32 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old August 24th, 2006, 08:14 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old August 24th, 2006, 08:21 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 30th, 2006, 08:52 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anyone ?

 
Old August 30th, 2006, 10:25 PM
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old September 2nd, 2006, 08:33 PM
Registered User
 
Join Date: Aug 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hotleave
Default

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");
}










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.