Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes switch statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch statement" Watch "switch statement" New topic

switch statement

adam faith

Joined: Aug 24, 2002
Posts: 22
just working through some code and came across a method with a switch statement in it
public boolean parse(char response)
case 'y':
case 'Y':
return true;

case 'n':
case 'N':
return false;
default: ;
but when i compile i keep getting the error
Error #: 466 : method does not return a value at line 57, column 3
i dont understand this from what i can see in the switch a boolean is bieng returned, can somebody help.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Think what happens when 'response' equals 'Q'. The switch would select the 'default' clause which doesn't do anything, so finally control flow hits the end of the function where no value is returned at all. That's what your compiler is complaining about.
kind regards
adam faith

Joined: Aug 24, 2002
Posts: 22
Hi Jos
so i added in
throw new IllegalArgumentException("response must be y or n");
and it works, thanks for the pointer
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Of course, if this is a response typed by a user, you'd better handle the IllegalArgumentException somewhere, or else the program will "abruptly terminate" (i.e. crash).

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Have you tried LearnNowOnline?
subject: switch statement