Solved

if else if else if    using ? :

Posted on 2011-03-12
4
288 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 18

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 18

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 18

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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
Requested array size exceeds VM limit 3 84
replace a word with other 1 40
couple of eclipse 5 19
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

919 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now