Thursday, 27 October 2016

java - loop break not working inside switch block on array of strings

Please refer the below code in which loop break does not work inside the switch block, can you help?



    String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };

//For loop is not breaking after getting the correct value
for (int t = 0; t < Books.length; t++) {
switch (Books[t]) {
case "Harry Potter":
System.out.println("Getting from switch case " + t + " " + Books[t]);
break;

default:
System.out.println("Invalid search for book from switch case");
break;
}
}

No comments:

Post a Comment