Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

if else if else if    using ? :

Posted on 2011-03-12
4
290 Views
Last Modified: 2012-06-21
hi guys

i have miltiple if else

like
if (user = manager)
{
user.name = 'Manager'
}
else if(user == CEO)
{
user.name = 'CEO'
}
else if(user == CLERK)
{
user.name = 'CLERK'
}

can i represent multiple If else conditions using ? :
for example

user == manager ? user.setName(MANAGER)  :  stuck here

any help appreciated

thakhs

0
Comment
Question by:royjayd
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 400 total points
ID: 35117508
user==manager ? user.setName(MANAGER) : (  (user == CEO) ?  ( user.setName('CEO') ): (  (user == CLERK) ?  (user.setName('CLERK')) : (   )   )     )
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35117527
SAME AS

user==manager ? user.setName(MANAGER) :  (user == CEO) ?   user.setName('CEO') :   (user == CLERK) ?  user.setName('CLERK') : ()
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35117536
user==manager ? user.setName("MANAGER") :  (user == CEO) ?   user.setName("CEO") :   (user == CLERK) ?  user.setName("CLERK") : ()
0
 
LVL 27

Expert Comment

by:dpearson
ID: 35117940
If you're doing this sort of nest ternary operator here's a way to lay it out so people can read it.  Also you can assign the result to a variable and then use that in a single method call - so there's less code to change if you rename "user.setName".
 
String name = 
   (user == manager) ? "MANAGER"
   : (user == CEO) ? "CEO"
     : (user == CLERK) ? "CLERK"
       : "" ;

user.setName(name) ;

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
hibernate jars 4 45
ejb stateless example 2 21
ejb on wildfly 5 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question