Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

9/2/2019 java - Using two values for one switch case statement - Stack Overflow

Make your voice heard. Take the 2019 Developer Survey now

Using two values for one switch case statement Ask Question

In my code, the program does


something depending on the text
202 entered by the user. My code looks
like:

switch (name) {
case text1: {
//blah
35 break;
}
case text2: {
//blah
break;
}
case text3: {
//blah
break;
}
case text4: {
//blah
break;
}

However, the code inside cases


text1 and text4 is the same. I was
therefore wondering if it would be
possible for me to implement
something like

case text1||text4: {
//blah
break;
}

I know that the || operator won't


work in the case statement but is
there something similar I can use.

java switch-statement

edited Jul 17 '18 at 2:20


Kaiido
41.5k 4 62 102

asked May 23 '13 at 6:10


Ankush
2,252 7 22 39

24 Being a basic question makes it if


anything more eligable for upvotes if
its not a duplicate as its widely useful.
And its something that didn't occure
to me as possible but now that I
realise it its blindingly obvious. So all
in all a pretty awesome Q&A –
Richard Tingle May 23 '13 at 12:58

1 @RichardTingle - are you familiar


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
with Duff's Device -
Terms of Service. en.wikipedia.org/wiki/Duff%27s_devic
e – user93353 May 24 '13 at 4:25
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 1/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

3 "Why so many upvotes? Search for


"java switch" in the internet and read
one of the thousand explanations." <-
- what do you think I was doing? –
Brendan Apr 12 '16 at 21:52

4 I literally searched for "multiple cases


in one line java" and this Q&A was
the first result. – domenix Sep 5 '16 at
8:09

1 The switch demo in the selected


answer could be rephrased now that
JDK-12 has integrated JEP-325. :) –
nullpointer Sep 8 '18 at 18:31

10 Answers

You can use have both CASE


statements as follows.
419
case text1:
case text4:{
//blah
break;
}

SEE THIS EXAMPLE:The code


example calculates the number of
days in a particular month:

class SwitchDemo {
public static void main(String[]

int month = 2;
int year = 2000;
int numDays = 0;

switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0)
!(year % 100 ==
|| (year % 400 =
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("I
break;
}
System.out.println("Number of
+ numDays)
}
}
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of ServiceThis
. is the output from the code:

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 2/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

Number of Days = 29

FALLTHROUGH:

Another point of interest is the


break statement. Each break
statement terminates the
enclosing switch statement.
Control flow continues with the
first statement following the
switch block. The break
statements are necessary
because without them,
statements in switch blocks fall
through : All statements after
the matching case label are
executed in sequence, regardless
of the expression of subsequent
case labels, until a break
statement is encountered.

EXAMPLE CODE:

public class SwitchFallThrough {

public static void main(String[]


java.util.ArrayList<String> f
new java.util.ArrayList<S

int month = 8;

switch (month) {
case 1: futureMonths.add
case 2: futureMonths.add
case 3: futureMonths.add
case 4: futureMonths.add
case 5: futureMonths.add
case 6: futureMonths.add
case 7: futureMonths.add
case 8: futureMonths.add
case 9: futureMonths.add
case 10: futureMonths.add
case 11: futureMonths.add
case 12: futureMonths.add
default: break;
}

if (futureMonths.isEmpty()) {
System.out.println("Inval
} else {
for (String monthName : f
System.out.println(mon
}
}
}
}

This is the output from the code:

August
September
October
November
December

Using Strings in switch Statements

In Java SE 7 and later, you can


use a String object in the switch
By using our site, you acknowledge
statement's that you have
expression. Theread and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. following code example, ,

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 3/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

displays the number of the month


based on the value of the String
named month:

public class StringSwitchDemo {

public static int getMonthNumber(

int monthNumber = 0;

if (month == null) {
return monthNumber;
}

switch (month.toLowerCase())
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}

return monthNumber;
}

public static void main(String[]

String month = "August";

int returnedMonthNumber =
StringSwitchDemo.getMonth

if (returnedMonthNumber == 0)
System.out.println("Inval
} else {
System.out.println(return
}
}
}

The output from this code is 8.

By using our site,FROM Java Docsthat you have read and understand our Cookie Policy, Privacy Policy, and our
you acknowledge
Terms of Service.

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 4/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
edited Feb 10 '14 at 6:44
Dennis Meng
4,678 9 27 35

answered May 23 '13 at 6:12


PSR
27.4k 30 87 128

oh ok. That was easy. Didn't know I


could do that – Ankush May 23 '13
at 6:14

14 It's worth to mention that this


language feature is called
fallthrough. Cases without break
are basically appended with next
case block which is visually below,
hence fall through. – Emperor Orionii
May 23 '13 at 7:40

5 @Kobor42 first learn how to talk in


public sites.Any how your suggestion
is help ful.Thanks – PSR May 23 '13
at 16:22

1 @Kobor42 How about: Why have


you used that formatting? Putting
cases horisontally makes the code
less readable and is generally
considered bad practice [Reference
optional but desired]. I have always
felt that switch statements are a
particularly readable format but
presented this way they lose all that.
– Richard Tingle May 25 '13 at 20:11

1 The switch demo could be rephrased


now that JDK-12 has integrated JEP-
325. :) – nullpointer Sep 8 '18 at
18:30

you can do like:

case text1:
32 case text4: {
//blah
break;
}

answered May 23 '13 at 6:12


Grijesh Chauhan
45.5k 14 96 162

for OP: read this also Java switch


cases: with or without braces? –
Grijesh Chauhan May 23 '13 at 6:25

The case values are just codeless


"goto" points that can share the same
23 entry point:

case text1:
By using our site,case
you acknowledge
text4: that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. //blah
break;

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 5/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

Note that the braces are redundant.

edited May 23 '13 at 12:15

answered May 23 '13 at 6:14


Bohemian ♦
296k 64 422 557

4 It's always a good thing when your


entry points are sane. – TRiG May 23
'13 at 10:19

@trig lol. I'm doing that kind of thing a


lot lately - blaming iPhone thumb
typing. Cheers – Bohemian ♦ May 23
'13 at 12:17

Just do

case text1: case text4:


16 do stuff;
break;

answered May 23 '13 at 6:13


kaljak
817 10 23

The fallthrough answers by others


are good ones.
5
However another approach would be
extract methods out of the contents of
your case statements and then just
call the appropriate method from
each case.

In the example below, both case


'text1' and case 'text4' behave the
same:

switch (name) {
case text1: {
method1();
break;
}
case text2: {
method2();
break;
}
case text3: {
method3();
break;
}
case text4: {
method1();
break;
}

I personally find this style of writing


By using our site, you statements
case acknowledgemore
that you have read and understand our Cookie Policy, Privacy Policy, and our
maintainable
Terms of Serviceand
. slightly more readable, especially

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 6/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

when the methods you call have good


descriptive names.

answered May 23 '13 at 12:52


Colin D
5,103 15 32

1 It is not more maintainable if text1


and text4 will ALMOST CERTAINLY
do the same thing, regardless of a
future change. If they should always
be linked, making a change in the
case for text1 (meaning changing
which method it calls) would require a
change in text4 . In this case it is
obviously not more maintainable. It
depends on the situation. –
Nick Freeman May 23 '13 at 21:49

1 I will say that this method should


probably be combined with the other
way anyway, since switch statements
are not (IMHO) the prettiest
programming structure. –
Nick Freeman May 23 '13 at 22:02

The brackets are unnecessary. Just


do
4
case text1:
case text4:
doSomethingHere();
break;
case text2:
doSomethingElse()
break;

If anyone is curious, this is called a


case fallthrough. The ability to do this
is the reason why break; is
necessary to end case statements.
For more information, see the
wikipedia article
http://en.wikipedia.org/wiki/Switch_sta
tement.

answered May 23 '13 at 7:15


scottmrogowski
1,020 1 12 29

Fall through approach is the best one


i feel.
2
case text1:
case text4: {
//Yada yada
break;
}

answered Jun 12 '13 at 11:21


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Ankur Sharma
Terms of Service. 163 1 11

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 7/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

With the integration of JEP 325:


Switch Expressions (Preview) in JDK-
2 12 early access builds, one can now
make use of the new form of the
switch label as :-

case text1, text4 -> {


//blah
}

or to rephrase the demo from one of


the answers, something like :-

public class RephraseDemo {

public static void main(String[] a


int month = 9;
int year = 2018;
int numDays = 0;

switch (month) {
case 1, 3, 5, 7, 8, 10, 12
numDays = 31;
}
case 4, 6, 9, 11 ->{
numDays = 30;
}
case 2 ->{
if (((year % 4 == 0) &
!(year % 100 =
|| (year % 400
numDays = 29;
else
numDays = 28;
}
default ->{
System.out.println("In

}
}
System.out.println("Number of
}
}

Here is how you can give it a try -


Compile a JDK12 preview feature
with Maven

edited Jan 3 at 12:19

answered Sep 8 '18 at 18:28


nullpointer
48.5k 11 101 195

The case values are just codeless


"goto" points that can share the same
1 entry point:

case text1:
case text4: {
//Do something
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
break;
Terms of Service.
}

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 8/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow

Note that the braces are redundant.

answered Mar 14 '14 at 5:58


David_DD
515 4 11

You can use:

case text1: case text4:


1 do stuff;
break;

answered Oct 25 '18 at 13:18


Joseph DSCS
11 1

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.

https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 9/9

You might also like