• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch case

 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to the chage this program. The Switches class uses a combination of an IF satatement and a SWITCH.I need to rewrite the buttin handler to use just a SWITCH statement.



I tried to change the programe using an another case in switch statement as :
Case 'Q' : case 'q' utWindow.dispose();
System.exit(0);

It did not work, it worked only when I passed the integers .But This is worng. Once I type "Q or q" in the textfield and press enter, the window should be closed.It is not happening so.

Please give me an idead ,how to change the program.
Thanks in advance.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only problem I see is that, assuming you removed the if test and added the extra cases as you described, if you enter "Q" but don't enter two numbers, the Integer.valueOf() calls will throw exceptions and you won't get to the switch statement at all.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code that you tried but didn't work. We can't tell you what you are doing wrong without knowing what you actually did in the first place!

Layne
 
Eddie Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For switch statements I think the variable:

char code;

Needs to be an integer constant for it to work in a switch statement.

So you can only use

public static final int code;

[ April 27, 2005: Message edited by: Eddie Lee ]
[ April 27, 2005: Message edited by: Eddie Lee ]
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For switch statements I think the variable:

char code;

Needs to be an integer constant for it to work in a switch statement.

So you can only use

public static final int code;


Not true. The switch expression can be char, byte, short, or int.

This:

works just fine.
 
Eddie Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the statement I made is implied anything that implicitly casts into an integer such as byte, char, short...

But based on what I've learned, the variable being tested must be:

public static and final.
[ April 27, 2005: Message edited by: Eddie Lee ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic