Why am I getting ” } ” error in switch statment below. I don’t see any syntax error.
I don’t get error on compiling the code. Eclipse is indicating error at the specified position below.
After putting } I get this error
- The static field KeyEvent.VK_XXXX should be accessed in a
static way
- case expressions must be constant expressions
switch(event.getKeyCode())
{
case event.VK_BACK_SLASH:
backColor=but[27].getBackground();
break;
case event.VK_RIGHT:
for(int i=0;i<but.length;i++)
{
if(" > ".equals(but[i].getText()))
{
backColor=but[i].getBackground();
break;
}
}
break; // error here
}
When I press the key, I get the follwoing error
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "}" to complete SwitchBlock
at Keyboard$HandlerClass.keyPressed(Keyboard.java:242) // this is break statement
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
Regards
The only problem I could see in your code is the following (but it’s not a compiletime/runtime error), which most probably would end up as a bug:
That’s not the right way to compare strings. Use
.equalsmethod insteadUpdate
That means you should change
event.VK_BACK_SLASHtoKeyEvent.VK_BACK_SLASH, …