# Switch case problem

Page 1 of 1

## 11 Replies - 2479 Views - Last Post: 23 September 2012 - 02:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=292798&amp;s=7def5e204d834497e198691fb273c96c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

# Switch case problem

Posted 22 September 2012 - 02:20 AM

```class wz
{
public static void main(String []args)
{

int convert; //the main object user wants to convert

int choicetemp; //chosing either from f to c or c to f
int celsiusvalue; //celsius value
int farvalue;//farenheit value

int choiceweight;//chosing from pds to kgs or kgs to pds
int cmvalue; //centimeter value
int inchvalue;// inch value

int choicelength; //inches to inche to cm or inverse
int pdvalue; //pound value
int kgvalue; // centimeter value

System.out.println("What would you like to convert?"); //allows user to choose what he wants to convert

System.out.println("1-Temperature.");

System.out.println("2-Length.");

System.out.println("3-Weight.");

convert=TextIO.getInt(); //allows user input
System.out.println(""); //empty line

switch (convert) //enabling switch case for general selection
{
case 1: //case 1 is the TEMPERATURE
System.out.println("Temperature Conversion");
System.out.println(".......................");
System.out.println("a. Celsius to Fahreinheit");
System.out.println("b. Fahrenheit to Celsius");
System.out.println("");
System.out.println("Please enter a choice");
choicetemp=TextIO.getChar(); //chosing a or b

switch (choicetemp) //enables the switch
{
case 'a': //what happens when he types a in
System.out.println("Enter Celsius value:");
celsiusvalue=TextIO.getInt(); //accepting celsius value
System.out.println(celsiusvalue + " Celsius = " + (celsiusvalue*33.8) + " Fahrenheit"); //the math equation to convert
break; //end the case
}
switch (choicetemp) //enables the switch
{
case 'b': //what happens when he types a in
System.out.println("Enter Fahrenheit value:");
farvalue=TextIO.getInt();
System.out.println(farvalue + " Fahrenheit = " + (farvalue-32)/1.8 + " Celsius");
break;
}

case 2: //LENGTH
System.out.println("Length Conversion");
System.out.println("..................");
System.out.println("a. Centimeters to Inches");
System.out.println("b. Inches to Centimeters");
System.out.println("");
System.out.println("Please enter a choice");

choicelength=TextIO.getChar(); //accepts if he wants either cm to inch of opposite

switch (choicelength) //turns on the switch case
{
case 'a': // what happens when he types a
System.out.println("Enter centimeter value");
cmvalue=TextIO.getInt();// accepts value for cm
System.out.println(cmvalue + " Centimeter = " + (cmvalue*0.39) + " Inch"); //takes previous value and calculate
break;

case 'b': // what happens when he types b
System.out.println("Enter Inche value");
inchvalue=TextIO.getInt();// accepts value for inch
System.out.println(inchvalue + " Inch = " + (inchvalue*2.54) + " Centimeter");
break;
}

case 3: //WEIGHT
System.out.println("Weigth Conversion");
System.out.println("...................");
System.out.println("a. Pounds to Kilograms");
System.out.println("b. Kilograms to Pounds");
System.out.println("");
System.out.println("Please enter a choice");

choiceweight=TextIO.getChar();

switch (choiceweight)
{
case'a':
System.out.println("Enter Pound value");
pdvalue=TextIO.getInt();
System.out.println(pdvalue + " Pound = " + (pdvalue*0.45) + " Kilogram");
break;

case 'b':
System.out.println("Enter Kilogram value");
kgvalue=TextIO.getInt();
System.out.println(kgvalue + " Kilograms = " + (kgvalue*2.2) + " Pound");
break;
}
break;// closes small switch
}//closes big switch

}//closes main
} //closes class

```

Hello everyone, I have a problem with this program that I made. When I type in all the required stuff, then it just post
```--------------------Configuration: <Default>--------------------
What would you like to convert?
1-Temperature.
2-Length.
3-Weight.
1

Temperature Conversion
.......................
a. Celsius to Fahreinheit
b. Fahrenheit to Celsius

Please enter a choice
? a
Enter Celsius value:
212
212 Celsius = 7165.599999999999 Fahrenheit
Length Conversion
..................
a. Centimeters to Inches
b. Inches to Centimeters

Please enter a choice
?

```

Normaly it should end after it said the temp in Fahrenheit, but then I get the length conversion printed. I recon it's problem with a break; but I just cannot find where it is. Any help would be greatly appreciated
Thanks you

Is This A Good Question/Topic? 0

## Replies To: Switch case problem

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Switch case problem

Posted 22 September 2012 - 02:53 AM

Give your classes better names that describe what they do, beginning with capital letters.

In at least one case, your switch construction is odd. It should be:
```switch (choicetemp) //enables the switch
{
case 'a': //what happens when he types a in
// do the 'a' stuff
break;

case 'b': //what happens when he types b in
// do the 'b' stuff
break;
}
```

Using nested switches - as you call them "big" and "small" switches - you're missing a break; after the "big" length switch.

You knew what was wrong. You should have trusted your instincts and found it.

### #3 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

## Re: Switch case problem

Posted 22 September 2012 - 02:59 AM

Thanks for your answer for the class name, I originaly called it converter, but then I wanted to try something so I created a new file so I wouldnt mess up my code. And the comment, is for my teacher, he wants a lot of them .
Now to the point i tried to do that but it gave me an "unreacheble statement".

### #4 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Switch case problem

Posted 22 September 2012 - 03:04 AM

Quote

i tried to do that but it gave me an "unreacheble statement"

I'm not sure what "that" is, but that's the code you should have posted and asked for help with.

### #5 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

## Re: Switch case problem

Posted 22 September 2012 - 03:09 AM

Yeah I know, but I didn't... Can you help me with the unreachable statement error?

### #6 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Switch case problem

Posted 22 September 2012 - 03:20 AM

Post it, and we'll fix it together.

### #7 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

## Re: Switch case problem

Posted 22 September 2012 - 06:27 AM

```class wz
{
public static void main(String []args)
{

int convert; //the main object user wants to convert

int choicetemp; //chosing either from f to c or c to f
int celsiusvalue; //celsius value
int farvalue;//farenheit value

int choiceweight;//chosing from pds to kgs or kgs to pds
int cmvalue; //centimeter value
int inchvalue;// inch value

int choicelength; //inches to inche to cm or inverse
int pdvalue; //pound value
int kgvalue; // centimeter value

System.out.println("What would you like to convert?"); //allows user to choose what he wants to convert

System.out.println("1-Temperature.");

System.out.println("2-Length.");

System.out.println("3-Weight.");

convert=TextIO.getInt(); //allows user input
System.out.println(""); //empty line

switch (convert) //enabling switch case for general selection
{
case 1: //case 1 is the TEMPERATURE
System.out.println("Temperature Conversion");
System.out.println(".......................");
System.out.println("a. Celsius to Fahreinheit");
System.out.println("b. Fahrenheit to Celsius");
System.out.println("");
System.out.println("Please enter a choice");
choicetemp=TextIO.getChar(); //chosing a or b

switch (choicetemp) //enables the switch
{
case 'a': //what happens when he types a in
System.out.println("Enter Celsius value:");
celsiusvalue=TextIO.getInt(); //accepting celsius value
System.out.println(celsiusvalue + " Celsius = " + (celsiusvalue*33.8) + " Fahrenheit"); //the math equation to convert
break; //end the case
}
switch (choicetemp) //enables the switch
{
case 'b': //what happens when he types a in
System.out.println("Enter Fahrenheit value:");
farvalue=TextIO.getInt();
System.out.println(farvalue + " Fahrenheit = " + (farvalue-32)/1.8 + " Celsius");
break;
}

case 2: //LENGTH
System.out.println("Length Conversion");
System.out.println("..................");
System.out.println("a. Centimeters to Inches");
System.out.println("b. Inches to Centimeters");
System.out.println("");
System.out.println("Please enter a choice");

choicelength=TextIO.getChar(); //accepts if he wants either cm to inch of opposite

switch (choicelength) //turns on the switch case
{
case 'a': // what happens when he types a
System.out.println("Enter centimeter value");
cmvalue=TextIO.getInt();// accepts value for cm
System.out.println(cmvalue + " Centimeter = " + (cmvalue*0.39) + " Inch"); //takes previous value and calculate
break;

case 'b': // what happens when he types b
System.out.println("Enter Inche value");
inchvalue=TextIO.getInt();// accepts value for inch
System.out.println(inchvalue + " Inch = " + (inchvalue*2.54) + " Centimeter");
break;
}

case 3: //WEIGHT
System.out.println("Weigth Conversion");
System.out.println("...................");
System.out.println("a. Pounds to Kilograms");
System.out.println("b. Kilograms to Pounds");
System.out.println("");
System.out.println("Please enter a choice");

choiceweight=TextIO.getChar();

switch (choiceweight)
{
case'a':
System.out.println("Enter Pound value");
pdvalue=TextIO.getInt();
System.out.println(pdvalue + " Pound = " + (pdvalue*0.45) + " Kilogram");
break;

case 'b':
System.out.println("Enter Kilogram value");
kgvalue=TextIO.getInt();
System.out.println(kgvalue + " Kilograms = " + (kgvalue*2.2) + " Pound");
break;
}
break;// closes small switch
}//closes big switch

}//closes main
} //closes class

```

It says:"error: break outside switch or loop"
Any ideas?
Thank you

Sorry i copied the wrong text.
```class wz
{
public static void main(String []args)
{

int convert; //the main object user wants to convert

int choicetemp; //chosing either from f to c or c to f
int celsiusvalue; //celsius value
int farvalue;//farenheit value

int choiceweight;//chosing from pds to kgs or kgs to pds
int cmvalue; //centimeter value
int inchvalue;// inch value

int choicelength; //inches to inche to cm or inverse
int pdvalue; //pound value
int kgvalue; // centimeter value

System.out.println("What would you like to convert?"); //allows user to choose what he wants to convert

System.out.println("1-Temperature.");

System.out.println("2-Length.");

System.out.println("3-Weight.");

convert=TextIO.getInt(); //allows user input
System.out.println(""); //empty line

switch (convert) //enabling switch case for general selection
{
case 1: //case 1 is the TEMPERATURE
System.out.println("Temperature Conversion");
System.out.println(".......................");
System.out.println("a. Celsius to Fahreinheit");
System.out.println("b. Fahrenheit to Celsius");
System.out.println("");
System.out.println("Please enter a choice");
choicetemp=TextIO.getChar(); //chosing a or b

switch (choicetemp) //enables the switch
{
case 'a': //what happens when he types a in
System.out.println("Enter Celsius value:");
celsiusvalue=TextIO.getInt(); //accepting celsius value
System.out.println(celsiusvalue + " Celsius = " + (celsiusvalue*33.8) + " Fahrenheit"); //the math equation to convert
break; //end the case
}
switch (choicetemp) //enables the switch
{
case 'b': //what happens when he types a in
System.out.println("Enter Fahrenheit value:");
farvalue=TextIO.getInt();
System.out.println(farvalue + " Fahrenheit = " + (farvalue-32)/1.8 + " Celsius");
break;
}

case 2: //LENGTH
System.out.println("Length Conversion");
System.out.println("..................");
System.out.println("a. Centimeters to Inches");
System.out.println("b. Inches to Centimeters");
System.out.println("");
System.out.println("Please enter a choice");

choicelength=TextIO.getChar(); //accepts if he wants either cm to inch of opposite

switch (choicelength) //turns on the switch case
{
case 'a': // what happens when he types a
System.out.println("Enter centimeter value");
cmvalue=TextIO.getInt();// accepts value for cm
System.out.println(cmvalue + " Centimeter = " + (cmvalue*0.39) + " Inch"); //takes previous value and calculate
break;

case 'b': // what happens when he types b
System.out.println("Enter Inche value");
inchvalue=TextIO.getInt();// accepts value for inch
System.out.println(inchvalue + " Inch = " + (inchvalue*2.54) + " Centimeter");
break;
}

case 3: //WEIGHT
System.out.println("Weigth Conversion");
System.out.println("...................");
System.out.println("a. Pounds to Kilograms");
System.out.println("b. Kilograms to Pounds");
System.out.println("");
System.out.println("Please enter a choice");

choiceweight=TextIO.getChar();

switch (choiceweight)
{
case'a':
System.out.println("Enter Pound value");
pdvalue=TextIO.getInt();
System.out.println(pdvalue + " Pound = " + (pdvalue*0.45) + " Kilogram");
break;

case 'b':
System.out.println("Enter Kilogram value");
kgvalue=TextIO.getInt();
System.out.println(kgvalue + " Kilograms = " + (kgvalue*2.2) + " Pound");
break;
}
break;// closes small switch
}//closes big switch
break;

}//closes main
} //closes class

```

### #8 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Switch case problem

Posted 22 September 2012 - 06:41 AM

The error message is referring to the break at line 115, right after your "//closes big switch" close brace and comment. Try commenting it out/removing it and see if that helps. The break for case 3 is at line 113, so your comment at that line is incorrect. If you have to make those comments, it would be better to refer specifically to the case being 'closed': 'closes case b', 'closes case 3', etc. to reduce confusion and improve code readability.

To my original comment: You're still missing break;s for cases 1 and 2.

### #9 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

## Re: Switch case problem

Posted 22 September 2012 - 08:16 AM

```class wz
{
public static void main(String []args)
{

int convert; //the main object user wants to convert

int choicetemp; //chosing either from f to c or c to f
int celsiusvalue; //celsius value
int farvalue;//farenheit value

int choiceweight;//chosing from pds to kgs or kgs to pds
int cmvalue; //centimeter value
int inchvalue;// inch value

int choicelength; //inches to inche to cm or inverse
int pdvalue; //pound value
int kgvalue; // centimeter value

System.out.println("What would you like to convert?"); //allows user to choose what he wants to convert

System.out.println("1-Temperature.");

System.out.println("2-Length.");

System.out.println("3-Weight.");

convert=TextIO.getInt(); //allows user input
System.out.println(""); //empty line

switch (convert) //enabling switch a case for general selection. SWITCH A
{
case 1: //case 1 is the TEMPERATURE
System.out.println("Temperature Conversion");
System.out.println(".......................");
System.out.println("a. Celsius to Fahreinheit");
System.out.println("b. Fahrenheit to Celsius");
System.out.println("");
System.out.println("Please enter a choice");
choicetemp=TextIO.getChar(); //chosing a or b

switch (choicetemp) //enables the switch SWITCH B
{
case 'a': //what happens when he types a in
System.out.println("Enter Celsius value:");
celsiusvalue=TextIO.getInt(); //accepting celsius value
System.out.println(celsiusvalue + " Celsius = " + (celsiusvalue*33.8) + " Fahrenheit"); //the math equation to convert
break; //end the case SWITCH B
}
switch (choicetemp) //enables the switch SWITCH C
{
case 'b': //what happens when he types a in
System.out.println("Enter Fahrenheit value:");
farvalue=TextIO.getInt();
System.out.println(farvalue + " Fahrenheit = " + (farvalue-32)/1.8 + " Celsius");
break; //SWITCH C
}

case 2: //LENGTH
System.out.println("Length Conversion");
System.out.println("..................");
System.out.println("a. Centimeters to Inches");
System.out.println("b. Inches to Centimeters");
System.out.println("");
System.out.println("Please enter a choice");

choicelength=TextIO.getChar(); //accepts if he wants either cm to inch of opposite

switch (choicelength) //turns on the switch case SWITCH D
{
case 'a': // what happens when he types a
System.out.println("Enter centimeter value");
cmvalue=TextIO.getInt();// accepts value for cm
System.out.println(cmvalue + " Centimeter = " + (cmvalue*0.39) + " Inch"); //takes previous value and calculate
break; //SWITCH D
}
switch (choicelength) //turns on the switch case SWITCH E
{
case 'b': // what happens when he types b
System.out.println("Enter Inche value");
inchvalue=TextIO.getInt();// accepts value for inch
System.out.println(inchvalue + " Inch = " + (inchvalue*2.54) + " Centimeter");
break; // SWITCH E
}

case 3: //WEIGHT
System.out.println("Weigth Conversion");
System.out.println("...................");
System.out.println("a. Pounds to Kilograms");
System.out.println("b. Kilograms to Pounds");
System.out.println("");
System.out.println("Please enter a choice");

choiceweight=TextIO.getChar();

switch (choiceweight) //SWITCH F
{
case'a':
System.out.println("Enter Pound value");
pdvalue=TextIO.getInt();
System.out.println(pdvalue + " Pound = " + (pdvalue*0.45) + " Kilogram");
break; //SWITCH F
}
switch (choiceweight) //turns on the switch case SWITCH G
{
case 'b':
System.out.println("Enter Kilogram value");
kgvalue=TextIO.getInt();
System.out.println(kgvalue + " Kilograms = " + (kgvalue*2.2) + " Pound");
break;//SWITCH G
}
break; //SWITCH A
}

}//closes main
} //closes class

```

Here, I tried doing what you suggested and renamed the switch so It would be clearer, I also added the breaks, but it didn't change anything.

Thanks

### #10 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Switch case problem

Posted 22 September 2012 - 08:22 AM

Where's the break; after case 1? After case 2? Give me line numbers.

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Switch case problem

Posted 22 September 2012 - 06:32 PM

Indent your code properly and it becomes obvious
Many time you repeat your switch with a single case for nothing

```    switch(xxx) {
switch(yyy) {
case 0:
dsfdfsdfsdf
break;
}
switch(yyy) {     // you already have a switch on yyy
case 1:       // this case should go inb previous switch()
dsfdfsdfsdf
break;
}

```

```class wz
{
public static void main(String []args)
{

int convert; //the main object user wants to convert

int choicetemp; //chosing either from f to c or c to f
int celsiusvalue; //celsius value
int farvalue;//farenheit value

int choiceweight;//chosing from pds to kgs or kgs to pds
int cmvalue; //centimeter value
int inchvalue;// inch value

int choicelength; //inches to inche to cm or inverse
int pdvalue; //pound value
int kgvalue; // centimeter value

System.out.println("What would you like to convert?"); //allows user to choose what he wants to convert

System.out.println("1-Temperature.");

System.out.println("2-Length.");

System.out.println("3-Weight.");

convert=TextIO.getInt(); //allows user input
System.out.println(""); //empty line

switch (convert) //enabling switch a case for general selection. SWITCH A
{
case 1: //case 1 is the TEMPERATURE
System.out.println("Temperature Conversion");
System.out.println(".......................");
System.out.println("a. Celsius to Fahreinheit");
System.out.println("b. Fahrenheit to Celsius");
System.out.println("");
System.out.println("Please enter a choice");
choicetemp=TextIO.getChar(); //chosing a or b

switch (choicetemp) //enables the switch SWITCH B
{
case 'a': //what happens when he types a in
System.out.println("Enter Celsius value:");
celsiusvalue=TextIO.getInt(); //accepting celsius value
System.out.println(celsiusvalue + " Celsius = " + (celsiusvalue*33.8) + " Fahrenheit"); //the math equation to convert
break; //end the case SWITCH B
}
switch (choicetemp) //enables the switch SWITCH C
{
case 'b': //what happens when he types a in
System.out.println("Enter Fahrenheit value:");
farvalue=TextIO.getInt();
System.out.println(farvalue + " Fahrenheit = " + (farvalue-32)/1.8 + " Celsius");
break; //SWITCH C
}

case 2: //LENGTH
System.out.println("Length Conversion");
System.out.println("..................");
System.out.println("a. Centimeters to Inches");
System.out.println("b. Inches to Centimeters");
System.out.println("");
System.out.println("Please enter a choice");

choicelength=TextIO.getChar(); //accepts if he wants either cm to inch of opposite

switch (choicelength) //turns on the switch case SWITCH D
{
case 'a': // what happens when he types a
System.out.println("Enter centimeter value");
cmvalue=TextIO.getInt();// accepts value for cm
System.out.println(cmvalue + " Centimeter = " + (cmvalue*0.39) + " Inch"); //takes previous value and calculate
break; //SWITCH D
}
switch (choicelength) //turns on the switch case SWITCH E
{
case 'b': // what happens when he types b
System.out.println("Enter Inche value");
inchvalue=TextIO.getInt();// accepts value for inch
System.out.println(inchvalue + " Inch = " + (inchvalue*2.54) + " Centimeter");
break; // SWITCH E
}

case 3: //WEIGHT
System.out.println("Weigth Conversion");
System.out.println("...................");
System.out.println("a. Pounds to Kilograms");
System.out.println("b. Kilograms to Pounds");
System.out.println("");
System.out.println("Please enter a choice");

choiceweight=TextIO.getChar();

switch (choiceweight) //SWITCH F
{
case'a':
System.out.println("Enter Pound value");
pdvalue=TextIO.getInt();
System.out.println(pdvalue + " Pound = " + (pdvalue*0.45) + " Kilogram");
break; //SWITCH F
}
switch (choiceweight) //turns on the switch case SWITCH G
{
case 'b':
System.out.println("Enter Kilogram value");
kgvalue=TextIO.getInt();
System.out.println(kgvalue + " Kilograms = " + (kgvalue*2.2) + " Pound");
break;//SWITCH G
}
break; //SWITCH A
}

}//closes main
} //closes class

```

### #12 Eliash

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 07-September 12

## Re: Switch case problem

Posted 23 September 2012 - 02:13 AM

Ok everyone I got it this time. Thanks a lot really appreciate it. Couldn't have done it without you.