Solved

if else if else if    using ? :

Posted on 2011-03-12
4
292 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 57
going to wrong jsp page 2 69
jsp CRUD operations with and without prepared statement also hibernatge 1 50
junit as external jar or library 7 58
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 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