user1781367
user1781367

Reputation: 602

Java IF ELSE syntax error

I'm having a java syntax error on IF ELSE statement. I reviewed my code several times but couldn't figure out what's wrong. The Error is : Syntax error on token "else", delete this token. What am I doing wrong here?

if(androidFragment!=null)
    ft.detach(androidFragment);

if(appleFragment!=null)
    ft.detach(appleFragment);

if(berryFragment!=null)
    ft.detach(berryFragment);


if(tabId.equalsIgnoreCase("android")){

    if(androidFragment==null){      

        ft.add(R.id.realtabcontent,new AndroidFragment(), "android");                       
    }else{

        ft.attach(androidFragment);                     
    }

}else if{   

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }

}else{  

    if(berryFragment==null){

        ft.add(R.id.realtabcontent,new BerryFragment(), "berry");                       
    }else{

        ft.attach(berryFragment);                       
    }

}

Upvotes: 0

Views: 1361

Answers (6)

Bibz
Bibz

Reputation: 321

You didn't use the else if construct properly, you code should look like :

}else if(tabId.equalsIgnoreCase("apple")) {   

    if(appleFragment==null){
        ...

}else if (tabId.equalsIgnoreCase("berry")){  

    if(berryFragment==null){
        ...
}

Upvotes: 1

rgettman
rgettman

Reputation: 178263

You can only have one else attached to an if; here you have two elses, for AppleFragment and BerryFragment.

if(tabId.equalsIgnoreCase("android")){

  ...
}else{  

    ...

}else{  

    ...

}

EDIT

You now have the following code fragment:

} else if {

Your else if requires a condition, e.g.

} else if (/*another boolean condition here for AppleFragment*/) {

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

You have no condition in this if

...
}else if{ /* condition missing at this if */  

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }
}...

Change it to what you need, possibly:

...
}else if (tabId.equalsIgnoreCase("apple")){

    if(appleFragment==null){

        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
    }else{

        ft.attach(appleFragment);               
    }
}...

Upvotes: 3

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

The problem is here :

else{   // Second else compilation error

    if(berryFragment==null){

        ft.add(R.id.realtabcontent,new BerryFragment(), "berry");                       
    }else{

        ft.attach(berryFragment);                       
    }

}

You cannot have two else statements.

Upvotes: 0

Sam
Sam

Reputation: 2970

Just one else is allowed, use else if

if(condition){ ....

}else if(condition 2){ ......

}else{ ........

}

Upvotes: 0

Marc B
Marc B

Reputation: 360702

You have 2 else's in a row

if (...) {
   ...
} else {
   ...
} else {   <--only one "else" allowed.
   ...
}

Upvotes: 1

Related Questions