Default statement position in switch - switch-statement

switch(2){
default : System.out.println("I am Default block");
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
}
The output : in 2
and if
switch(2){
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
default : System.out.println("I am Default block");
}
And the output is: in 2
I am Default block.
Does the positioning of the default statement behaves differently?

It's not the position of the default block, it's the use (or non-use in your case) of the break statement.
Once a case has been matched, the default behaviour is for the code to cascade through the rest of the cases.
To achieve the behaviour you desire, try
switch (2)
{
case 1:
System.out.println("in 1");
break;
case 2:
System.out.println("in 2");
break;
default:
System.out.println("I am Default block");
break;
}

Kathy Siera SCJP book do say that position of default: matters and if present in between the case statements, it follows the fall-through.
But practically it does not follow.
int a = 10;
switch (a)
{
case 5:
System.out.println("Its 5");
default:
System.out.println("Its default");
case 10:
System.out.println("Its 10");
case 20:
System.out.println("Its 20");
}
Output:
Its 10
Its 20

Related

Why is this switch statement only returning the default?

switch (numYears) {
case 10:
intsRate = 0.06;
break;
case 15:
intsRate = 0.05;
break;
case 30:
intsRate = 0.04;
break;
default:
intsRate = 0.08;
break;
}
return intsRate;
}
when the input of numYears is 10, 15, or 30 its returns the according double, but its only returning the defult. its part of a larger code but everything in that code is right this is the only part returning the wrong thing.

Java - Default Switch Statement

I am writing some tests for a Java method which contains a switch statements but it seems that the "default statement" does not work. I only accept: Yes, No, Maybe. Everything else should return Maybe. My test always return the user input, does not matter what the user typed, so I guess my switch statement is not correct.
I have tried to move the default statement on the top
#Override
public String choice(String ans) {
getChoice = ans;
switch (ans) {
case "Yes":
break;
case "No":
break;
default:
getChoice = "Maybe";
}
return getChoice;
}
Thank you!
Your switch should work... As Kyle tells you "what are you overriding ?"
But why a switch when you can perform an "if then else" ?
Public String choice (String choice) {
If (choice.equals("yes") || choice.equals("no") {
return choice;
else {
return "maybe";
}
}
Switch is fine for multiple choices

a groovy switch case statement with several variables

Is it possible to have a switch-case statement with more than a variable in groovy? I tried with tuples but the case part doesn't accept more than one argument.
I am trying to avoid several nested if statements so instead of
if (a==1) {
if (b==2) {
if (c==3) {
// do something
}
}
}
else {
if (a==4) {
if (b==5) {
if (c==6) {
//do something else
}
}
}
}
Can I do:
switch(a,b,c) {
case : (1,2,3) // if a==1, b==2 and c==3
// do something
...
case : (4,5,6)
// do something else
...
}
}
Groovy is just dirty java, you don't need any class definition. everything you write in a java method you can write it directly in groovy.
switch (num) {
case 1:
case 2:
case 3:
System.out.println("1 through 3");
break;
case 6:
case 7:
case 8:
System.out.println("6 through 8");
break;
}
To answer your question, inside the switch we need an expression, not function parameters.
Based on your edit, I believe that this should work:
if (a == 1 && b == 2 && c == 3) {
// do something
} else if (a == 4 && b == 5 && c == 6) {
// do something else
}
If you want a switch statement instead, that's possible:
def val = [a, b, c]
switch (val) {
case {it == [1, 2, 3]}:
// something
break;
case {it == [4, 5, 6]}:
// something else
break;
class Solution{
static void main (String...args){
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in))
def val=br.readLine()
switch(val){
case('E0'):
println "Basic"
break;
default:
break;
case('E1'):
println "Inter"
break;
case('E2'):
println "Advance"
break;
default:
println "not defined"
}
}
}

Jcombobox and switch case

I want to link my combobox selected item with switch case which result in different result depend on option chosen. But return in this error :Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to java.awt.event.ItemListener. Can only one help me, thanks you so much!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1.addItemListener((ItemListener) jComboBox1);
int usertype = (int) jComboBox1.getSelectedIndex();
switch (usertype) {
case '1':
new ResidentView().setVisible(true);
this.dispose();
break;
case '2':
new OfficeClerkView().setVisible(true);
this.dispose();
break;
case '3':
new OfficeManagerView().setVisible(true);
this.dispose();
break;
}

(beginner) How to use a switch statement with strings?

I'm trying to make a simple bank account program to learn classes and OOP. As you may guess, I'm new to Java.
Anyways, my switch statement is not working. I'm trying to make each case based on an inputted string.
Scanner input = new Scanner(System.in);
System.out.println("Enter your name");
//the user enters "user1", "user2", or "user3".
String user = input.next();
//swtich time
switch (user) {
case "user1":
System.out.println("Your balance is" + user1.balance);
System.out.println("Your Account numer is" + user1.acctnum);
//shows the balance and account number for user1
case "user2":
System.out.println("Your balance is" + user2.balance);
System.out.println("Your Account numer is" + user2.acctnum);
case "user3":
System.out.println("Your balance is" + user3.balance);
System.out.println("Your Account numer is" + user3.acctnum);
}
You're missing a break statement at the end of each case.
case "user1":
System.out.println("Your balance is" + user1.balance);
System.out.println("Your Account numer is" + user1.acctnum);
//shows the balance and account number for user1
break;
Without the break statement, all of these statements will be executed
Docs
You can use Strings in switch statements if you are using Java 7 or above, otherwise you can't
You can enumerate the string, and then use switch.
P.S: Please search around a bit and then post questions here ;) Your question is similar to this one (for ex - there are plenty of answers out there)
https://stackoverflow.com/a/338284/878170
You are missing break; statement and hence all statements after selected case will be executed.
Here's an example of switch-case [1]
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
[1] http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Resources