Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Orphaned Case

807601Apr 23 2008 — edited Apr 23 2008
Hi, I'm supposed to be writing a program that changes the state of a machine according to the inputs that it received, I decided to use switch statments to read the input and produce the output from it. When I compile I get the error message

"Controller.java:103: orphaned case
case COPY:
1 error"

I vaguely understand the concept of an orphaned case but can't see what Ive done wrong. Please help!
public class Controller {

 static public void  main(String[] args) {
  //MASKS
  final byte ON_SWITCH = (byte)0x01;
  final byte CARD_IN_OUT = (byte)0x03;
  final byte COPY_MASK = (byte)0x07;
  final byte RESET = (byte)0x00;

  //OUTPUTS
  final byte NO_LIGHTS = (byte)0xff;
  final byte ON_LED = (byte)0xfe;
  final byte ON_CARD_LED = (byte)0xfc;
  final byte ON_ERROR_LED = (byte)0xf6;
  final byte ON_CARD_ERROR_LED = (byte)0xf4;
  final byte ON_CARD_COPY = (byte)0xf8;
  final byte COPY_25_LED = (byte)0x74;
  final byte COPY_50_LED = (byte)0xb4;
  final byte COPY_75_LED = (byte)0xd4;
  final byte COPY_100_LED = (byte)0xe4;

  //STATES
  final int OFF = 0;
  final int ON = 1;
  final int CARD_IN = 2;
  final int COPY = 3;
  final int COPY_25 = 4;
  final int COPY_50 = 5;
  final int COPY_75 = 6;
  final int COPY_100 = 7;
  final int ERROR = 8;
  final int RESET_STATE  = 9;

  // instantiate I2C ports

  I2CPort writePort = new I2CPort();
  I2CPort readPort = new I2CPort();

  // set the addresses of the I2C ports

  writePort.setAddress((byte) 0x20);
  readPort.setAddress((byte) 0x21);

  // supplied read/write methods use arrays
  // in this case we read and write a single byte

  byte[] input = new byte[1];
  byte[] output = new byte[1];

  try {
   // loop forever
   while(true) {
    // read the input byte.

    int result = readPort.read(input, 0, 1);
    if (result !=0) {
     System.out.println("Unable to read from device: "+ result);
     System.exit(0);
    }
    // Read the switch value and set the output

    int switchValues;   
    int currentState = OFF; 
    output[0]=NO_LIGHTS;

    switch(currentState) {
     case OFF:
      output[0]=NO_LIGHTS;
      switchValues = input[0]&ON_SWITCH;
      switch(switchValues) {
      case 1:
       currentState = ON; break;
      } break;

     case ON:
      output[0]=ON_LED;
      switchValues = input[0];
      switch(switchValues) {
       case 0:
        currentState = OFF; break;
       case 3:
        currentState = CARD_IN; break;
      } break;

     case CARD_IN:
      output[0]=ON_CARD_LED;
      switchValues = input[0]&COPY_MASK;
      switch(switchValues) {
       case 1:
        currentState = ON; break;
       case 2:
        currentState = OFF; break;
       case 7:
        currentState = COPY; break;
      } break;

     try {

      case COPY:
       output[0]=ON_CARD_COPY;
       switchValues = input[0];
       switch(switchValues) {
        case 3:
         currentState = CARD_IN; break;
        case 6: case 5:
         currentState = ERROR; break;
        default:
         Thread.sleep(1000);
         currentState = COPY_25; break;
       } break;

      case COPY_25:
       output[0]=COPY_25_LED;
       switchValues = input[0];
       switch(switchValues) {
        case 5: case 6:
         currentState = ERROR; break;
        case 15:
         currentState = COPY; break;
        default:
         Thread.sleep(1000);
         currentState = COPY_50; break;
       } break;

      case COPY_50:
       output[0]=COPY_50_LED;
       switchValues = input[0];
       switch(switchValues) {
        case 5: case 6:
         currentState = ERROR; break;
        case 15:
         currentState = COPY; break;
        default:
         Thread.sleep(1000);
         currentState = COPY_75; break;
       } break;

      case COPY_75:
       output[0]=COPY_75_LED;
       switchValues = input[0];
       switch(switchValues) {
        case 5: case 6:
         currentState = ERROR; break;
        case 15:
         currentState = COPY; break;
        default:
         Thread.sleep(1000);
         currentState = COPY_100; break;
       } break;

      case COPY_100:
       output[0]=COPY_100_LED;
       switchValues = input[0];
       switch(switchValues) {
        case 5: case 6:
         currentState = ERROR; break;
       } break;
      }

     catch (Exception e) {
      System.err("Error Occurred");
     }


    }

    if(currentState == ON) {
     output[0]=(byte)0xfe;
     writePort.write(output,0,1);
    }

    // write the output
    result = writePort.write(output,0,1);

    if (result !=0) {
     System.out.println("Unable to write to device "+ result);
     System.exit(0);
    }
   }
  }
  catch (IllegalAddressException e) {
   System.out.println(e);
   System.exit(0);
  }

 }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2008
Added on Apr 23 2008
3 comments
200 views