GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch case" Watch "Switch case" New topic
Author

Switch case

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Eddie Lee
Greenhorn

Joined: Apr 27, 2005
Posts: 20
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

Joined: Jul 10, 2002
Posts: 214

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.


SCJP, SCWCD
Eddie Lee
Greenhorn

Joined: Apr 27, 2005
Posts: 20
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 ]
 
GeeCON Prague 2014
 
subject: Switch case