Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Switch case

 
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.
 
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.
 
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
 
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 ]
 
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 ]
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic