Program stuck in nested switch [duplicate] - java

Why is it that the following code:
class swi
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");continue;}
case 'b':{ System.out.println(a); continue;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
Gives the error:
continue outside of loop

Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.
Based on my understanding of your intentions, you probably want to write:
System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
System.out.println(a);
}
I would also suggest that you place the default condition at the very end.
EDIT:
It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continue statement is entirely valid. For example:
public class Main {
public static void main(String[] args) {
loop:
for (int i=0; i<10; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 9:
continue loop;
}
System.out.println(i);
}
}
}
This will produce the following output:
0
2
4
6
8

The continue-Statement may be used in loops and not in switch. What you probably want is a break.

Because you have a continue outside of a loop. continue is for jumping to the next iteration of a loop, skipping the remainder of the current iteration. But you don't have any loop in that code. What you want for breaking out of a switch case block is the keyword break (see below).
There's also no need to put every case block within braces (unless you want locally-scoped variables within them).
So something a bit like this would be more standard:
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:
System.out.println("default");
break;
case 'b':
System.out.println(a);
break;
case 'a':
System.out.println(a);
break;
}
System.out.println("Switch Completed");
}
}
There's also a school of thought that says the default condition should always be at the end. This is not a requirement, just a fairly widely-used convention.

Shouldn't you use break instead of continue?

continue simply moves directly to the next iteration of the loop.
break is used to break out of loops and switches.
Use break; instead of continue;
Continue:
for(x = 0; x < 10; x++)
{
if(x == 3)
continue;
else
DoIterativeWork();
}
Switch:
switch(a)
{
default:{ System.out.println("default"); break;}
case 'b':{ System.out.println(a); break;}
case 'a':{ System.out.println(a);}
}

You are using continue where you should be using break

continue inside switch??!!! only break can be kept inside switch.!
ur code should be,
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");break;}
case 'b':{ System.out.println(a); break;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}

Related

How can I make the program stop after entering a value from a user?

I am trying to write a program that receives the number of sides from the
user and determines the type of figure using switch structure and a while sentinel-controlled loop, but every time I get an infinite loop. How can that be fixed?
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of sides:");
int s = input.nextInt();
while ( s!=-1)
{
switch (s)
{
case 1: System.out.println("Line");
break;
case 2:System.out.println("Angle");
break;
case 3:System.out.println("Triangle");
break;
case 4:System.out.println("Quadrilateral");
break;
case 5:System.out.println("Pentagon ");
break;
case 6:System.out.println("Hexagon");
break;
case 7:System.out.println("Heptagon");
break;
case 8:System.out.println("Octagon");
break;
case 9:System.out.println("Nonagon");
break;
case 10:System.out.println("Decagon");
break;
default: System.out.println("Enter a valid value:");
}
}
}
}
The while loop is written to continue as long as s!=-1; so you need to change s so that this expression is no longer true.

Modification of value in switch case

public class Sample {
public static void main(String[] args) {
int i = 9;
switch (i) {
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
}
}
}
Output:
default
zero
Although i was initialized as 9, how is it possible that case 0 was picked? What is the reason?
You miss a break statement after the first default case so the excution just falls through to the second case.Also put the Default case last so that it is only excuted after all the other cases are checked.
public static void main(String[] args) {
int i = 9;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
break;
}
}
When I ran this, it printed:
default
zero
You have a couple things wrong.
(1) Put the default case at the end, not beginning. Otherwise you'll automatically go into the default case.
(2) Put a break statement after each case. If you don't, you code will continue from the case without the break and execute the code from the next case (and the next) until it reaches a break.
Try this
public class Sample {
public static void main(String[] args) {
int i = 9;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
}
}
}
Link on Java switch statements --http://www.tutorialspoint.com/java/switch_statement_in_java.htm.
You missed using break in your default case thus the program continues to execution and prints "zero".
You are doing two mistakes.
you should place the default case always at the end
you are missing important breaks
Solution:
public class Sample
{
public static void main(String[] args)
{
int i = 9;
switch (i)
{
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
}
}
}

change a phone number as a string into all numbers

Im writing a program in java that is suppose to receive a phone number as a string, (ex. 1800helpmee) from the user and print out its corresponding numbers. 1800 435 7633 should be the answer. this is the code i have so far. I've loaded the number into the array. I am having an issue with the next part. my array is called inputNumber. I tried something like this but it doesn't seem to be working
for (int j = 0; j<9; j ++) {
if ( inputNumber[j] =='A' || inputNumber[j] == 'B' || inputNumber[j] =='C'){
System.out.println("2");
} etc.
switch inputNumber[j] =='A' to inputNumber[j].equalsIgnoreCase("a") apply this for a, b and c then retry
You could use a for each loop to iterate the strings array
public static void main(String[] args) {
String [] inputNumber = {"1","8","0","0","h","e","l","p"};
for (String number : inputNumber) {
switch (number.toUpperCase()) {
case "A":
case "B":
case "C":
System.out.println("2");
break;
case "D":
case "E":
case "F":
System.out.println("3");
break;
// other letters
default:
System.out.println(number);
}
}
}
Evaluate each letter with a switch (as of Java 7) and print the aproppriate number. Notice the fall through, a break statement after several case. The toUpper() method is called to compare only with the uppercase version of each letter.

What's wrong with the switch-case statement?

I do not why eclipse markes the switch-case statement with red squiggle, an says Syntax error on token "{", SwitchLabels expected after this token I tried both of the below posted code and i receive the same complain.
Code_1
switch (Test.h1.size()) {
int size = Test.h1.size();
case 1:
break;
case 2:
break;
}
Code_2
switch (Test.h1.size()) {
int size = Test.h1.size();
case size == 1:
break;
case size == 2:
for (int i=1; i<=Test.h1.size()-1; i++) {
for (int j=i+1; j<=Test.h1.size(); j++) {
System.out.println( Test.h1.get(i)+"+"+Test.h1.get(j)+"= "+((Test.h1.get(i))+(Test.h1.get(j))) );
}
}
break;
}
All your code needs to be in cases. Did you read the docs? http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Try this:
int size = Test.h1.size();
switch (size) {
case 1:
break;
case 2:
....;
}
To solve your problem in code 1 you only have to remove the line int size = Test.h1.size();.
For code 2 you should know, that in Java you cannot use boolean expressions in switch cases.

How do I return to the top of a switch statement if the input goes to default?

I was doing my homework (Generate Roman Numerals from numerical input from 1-10), and in doing so, I made a switch statement. My question is how do I return to the top of a switch statement if none of the input is selected in a case? There was nothing about it in our textbook nor could I find anything in the java documentation. Is this just something that cannot happen?
Personally I prefer the while loop to do this:
just to explain the basic idea: you initialize a boolean that will be the criteria whether the loop has to be repeated, you start the while loop and put the boolean at false because in most cases you only want to run it once, you start the switch and for all cases where you want to repeat the loop you put the boolean again equal to true.
boolean again= true;
while (again){
again= false;
switch(number){
case 1:
break;
case 2:
break;
default: again=true;
break;
}
}
you can break from in case or can write return statement if it is method.
public int method(int i){
int j=0;
switch(i){
case 1: ... return j;
....
}
}
Here is how to do it but I highly recommend you to restructure your code to avoid doing it.
public class Main {
public static void main(String[] args) {
int i = 0;
loop: for (;;) {
switch (i) {
case 1: System.out.println(i);
break loop;
case 2: // more stuff
break loop;
default:
System.out.println(i);
i = 1;
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
int i=3;
switch (i) {
default:
i=1;
case 1:
System.out.println(i);
break;
case 2:
break;
}
}
}

Categories