Home arrow static arrow Java Programming [Archive] - one line if statement
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - one line if statement
This topic has 5 replies on 1 page.

Posts:41
Registered: 4/21/02
one line if statement  
Aug 4, 2004 10:15 AM



 
I am trying to use the one line if statement but I am not doing it correctly (it is not compiling). Here is the code:

String record = "Bob", "32", "Dog"; StringTokenizer tokenizer = new StringTokenizer(record, ","); list.add(new Person(tokenizer.nextToken(),                          tokenizer.nextToken(),                          (tokenizer.nextToken()).equals("Dog")?                         new Dog():new Cat()));


I am adding a Person object to an ArrayList. The Person constructor looks like this:

public Person(String name, String age, Pet pet){}


if the last token is "Dog", I want to pass to the Person constructor a Dog object, it is "Cat" I want to pass in a Cat object. Cat and Dog are subclasses of Pet.

The compile error is:

incompatible types for ?: neither is a subtype of the other
second operand: Dog
third operand : Cat
(tokenizer.nextToken()).equals("Dog")?

What am I doing wrong or can I even use the short hand if statement in this case?
Thanks.
 

Posts:6,750
Registered: 1/25/04
Re: one line if statement  
Aug 4, 2004 10:55 AM (reply 1 of 5)



 
JLS section 15.25: "If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (�5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type."

In other words, you'll have to declare a Pet variable, and assign to it a Dog or a Cat depending on your token.
 

Posts:112
Registered: 4/13/98
Re: one line if statement  
Aug 4, 2004 11:00 AM (reply 2 of 5)



 
Cast each of the objects to type Pet:

tokenizer.nextToken()).equals("Dog") ? (Pet)(new Dog()) : (Pet)(new Cat())

For an explanation of why this is necessary, look at the following:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293

 

Posts:6,750
Registered: 1/25/04
Re: one line if statement  
Aug 4, 2004 11:01 AM (reply 3 of 5)



 
Cast each of the objects to type Pet:

D'oh, why didn't I think of that?
 

Posts:10,972
Registered: 10/23/03
Re: one line if statement  
Aug 4, 2004 11:04 AM (reply 4 of 5)



 
Works for me (using 1.5.0):
public class Animal {    public static void main(String[] args) {        f(args.length < 1 ? new Dog() : new Cat());    }     public static void f(Animal a) {    }} class Dog extends Animal {}class Cat extends Animal {}
 

Posts:3,258
Registered: 00-08-28
Re: one line if statement  
Aug 4, 2004 11:17 AM (reply 5 of 5)



 
Just wondering. Though it might not be applicable under your circumstances but have you considered factory pattern
 
This topic has 5 replies on 1 page.