Solved

if else if else if    using ? :

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
topping3 challenge 14 80
Windows 10 IE Certificate Issue 10 45
Configure a Bean in an XML file 4 31
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

777 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