12 Replies - 14071 Views - Last Post: 23 June 2011 - 09:21 PM Rate Topic: -----

#1 jyablos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-June 11

Orphaned Case Error

Posted 23 June 2011 - 05:16 AM

im trying to do this very simple code. but i got an orphaned case error

im using CMD with JDK/Netbeans

this is my code.
class NestedIfSample
{
public static void main(String args[])
throws Exception
{ 
System.out.println("Enter [A] ADD\n [S]SUB\n[M]MUL\n[D]DIV");
Char choice=(Char)system.read();
if (choice =='A');
case 'S':
System.out.println("Subtracting");
break;
case 'M':
System.out.println("Multiplying");
break;
case 'D':
System.out.println("Dividing");
break;
default:System.out.println("invalid choice");
System.out.println("Statement after the switch");
}
}



the error says

orphaned error

on case 'S':

ve tried alot but im so new at this. i dont have any experience on programming before i hope someone could help
thanks

This post has been edited by macosxnerd101: 23 June 2011 - 06:01 AM
Reason for edit:: Added code tags and renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Orphaned Case Error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 05:30 AM

The error message is complaining that you have a case statement outside of a switch block, which you do. You can't use case outside of a switch block. You need to surround the cases with switch(choice) { ... }. How else should Java know that you want to compare against choice?

Also it seems strange that you use an if statement instead of a case statement for the "A" case and that that case is empty.

This post has been edited by sepp2k: 23 June 2011 - 05:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1992
  • View blog
  • Posts: 4,143
  • Joined: 11-December 07

Re: Orphaned Case Error

Posted 23 June 2011 - 06:09 AM

Yeah, a switch..case statement should look like this:


			char option = // set it to something
			switch (option) {
			case 'a':
				// do something
				break;
			case 'b':
				// do something else
				break;
			case 'c':
				// do something different again
				break;
			default:
				// do this if no matches
			}



When I'm learning a new programming language, I like to keep a cheat sheet of the important language features like loops and control flow. You could make your own or print out this one here:

http://www.dreaminco...ce-cheat-sheet/
Was This Post Helpful? 1
  • +
  • -

#4 jyablos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 06:16 AM

thanks guys i think i sort it out, and i feel good because this is the first code ive done.
class SwitchSample
{
public static void main(String args[])
throws Exception
{ 
System.out.println("Enter [A] for ADD [S] for SUB [M] for MUL [D] for DIV");
char choice=(char)System.in.read();
switch(choice){
case 'A':
System.out.println("Addition");
break;
case 'S':
System.out.println("Subtracting");
break;
case 'M':
System.out.println("Multiplying");
break;
case 'D':
System.out.println("Dividing");
break;
default:System.out.println("invalid choice");

}
}
}


thanks!

now im trying to put an exception or should i say if lowercase letter is input it will be converted to uppercase so it will still accept it.

and im pretty confused where to put the code

This post has been edited by macosxnerd101: 23 June 2011 - 06:26 AM
Reason for edit:: Please use code tags.

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1992
  • View blog
  • Posts: 4,143
  • Joined: 11-December 07

Re: Orphaned Case Error

Posted 23 June 2011 - 06:19 AM

If you convert the character to upper case before you check it then your existing code will cover both upper case and lower case. There is a method in class Character which ensures it will be upper case.
Was This Post Helpful? 2
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Orphaned Case Error

Posted 23 June 2011 - 06:27 AM

You can also combine cases for upper and lower case letters like so:
case 'a':
case 'A':
    //addition
    break;

case 's':
case 'S':
   //subtraction
   break;


Was This Post Helpful? 1
  • +
  • -

#7 jyablos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 06:42 AM

ive tried what macosxnerd101ntold me and it really works thanks alot from that.

class SwitchSample
{
public static void main(String args[])
throws Exception
{ 
System.out.println("Enter [A] for ADD [S] for SUB [M] for MUL [D] for DIV");
char choice=(char)System.in.read();
switch(choice){
char toUpperCase(char ch)
case 'A':
case'a':
System.out.println("Addition");
break;
case 'S':
case's':
System.out.println("Subtracting");
break;
case 'M':
case'm':
System.out.println("Multiplying");
break;
case 'D':
case'd':
System.out.println("Dividing");
break;
default:System.out.println("invalid choice");

}
}
}


but now im trying to figure out how to use the "toUppercase" function in to this.
cause i really want to learn, im so excited about this,
im not sure if its possible so, can anyone tell me if it is possible to put touppercase inside a switch? or it should be before the switch code?

This post has been edited by jyablos: 23 June 2011 - 06:43 AM

Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Orphaned Case Error

Posted 23 June 2011 - 06:43 AM

You have to use the Character.toUpperCase() method before the switch. You cannot put it in the switch block, unless it is in a case (which doesn't make much sense, though).
char choice = Character.toUpperCase(someChar);
switch(choice){

}


Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 06:48 AM

Sure you can put it in a switch. Just do:

switch(Character.toUpperCase(choice)) {
    ...
}


Was This Post Helpful? 0
  • +
  • -

#10 jyablos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 07:25 AM

guys im did some surfing and research over google but still no luck.
the compiler wont show any errors but it wont conver lower case to upper case

heres my code

class SwitchSample
{
public static void main(String args[])
throws Exception
{ 

System.out.println("Enter [A] for ADD [S] for SUB [M] for MUL [D] for DIV");
char choice=(char)System.in.read();
char ch = Character.toUpperCase('a'); 
     //here is what i put to do the convertion
switch(choice){
case 'A':
System.out.println("Addition");
break;
case 'S':
System.out.println("Subtracting");
break;
case 'M':
System.out.println("Multiplying");
break;
case 'D':
System.out.println("Dividing");
break;
default:System.out.println("invalid choice");

}
}
}


i wanted to convert a to A so it will still shot an output of "Adding"
can anyone help me out so i can finally sleep lol!

thanks

This post has been edited by jyablos: 23 June 2011 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 07:29 AM

You're storing the upper-cased letter in ch and then switching on choice (which contains the original letter). You want to switch on ch instead (or get rid of ch and switch directly on Character.toUpperCase(choice) instead).

This post has been edited by sepp2k: 23 June 2011 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#12 jyablos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-June 11

Re: Orphaned Case Error

Posted 23 June 2011 - 09:44 AM

thanks alot guys after a couple of hourse sorting it out i finally got it right

here it is



class SwitchSample
{
public static void main(String args[])
throws Exception
{

System.out.println("Enter [A] for ADD [S] for SUB [M] for MUL [D] for DIV");
char choice=(char)System.in.read();
char ch = Character.toUpperCase(choice);
switch(ch){
case 'A':
System.out.println("Addition");
break;
case 'S':
System.out.println("Subtracting");
break;
case 'M':
System.out.println("Multiplying");
break;
case 'D':
System.out.println("Dividing");
break;
default:System.out.println("invalid choice");
	 
}
}
}

Was This Post Helpful? 1
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Orphaned Case Error

Posted 23 June 2011 - 09:21 PM

Sorry invalid Java syntax... please test your code (at least to see if it compiles) before posting it

View Postjyablos, on 23 June 2011 - 09:42 AM, said:

ive tried what macosxnerd101ntold me and it really works thanks alot from that.

class SwitchSample
{
public static void main(String args[])
throws Exception
{ 
System.out.println("Enter [A] for ADD [S] for SUB [M] for MUL [D] for DIV");
char choice=(char)System.in.read();
switch(choice){
char toUpperCase(char ch)     // invalid statemnent in a swicth()
case 'A':
case'a':
System.out.println("Addition");
break;
case 'S':
}

And please indent your code correctly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1