Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch statement

 
adam faith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
just working through some code and came across a method with a switch statement in it
public boolean parse(char response)
{
switch(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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jos
so i added in
default:
throw new IllegalArgumentException("response must be y or n");
and it works, thanks for the pointer
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic