missing return statement...

A single upper case character is supplied to isHex() to return true it is hex 0 - F.

Q1: line public static boolean IsHex(String str) {  complains saying "missing return statement", why?

Q2: Is there a better way ofwriting this if statement?

Thank you.
public static boolean IsHex(String str) {
 
            if(str.equals("0") || str.equals("1") || str.equals("2") ||
               str.equals("3") || str.equals("4") || str.equals("5") ||
               str.equals("6") || str.equals("7") || str.equals("8") ||
               str.equals("9") || str.equals("A") || str.equals("B") ||
               str.equals("C") || str.equals("D") || str.equals("E") ||
               str.equals("F")) 
                return true;    
    }

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
for_yanCommented:
Well you can do like that

if("0123456789ABCDEF".indexOf(str) > -1)return true;
else return false;

but  you need to be sure that you string str would not be of two chars

you can say:

if("0123456789ABCDEF".indexOf(str) > -1  && str.length() == 1)return true;
else return false;


0
 
for_yanCommented:
because if it is not true
there is no returtn statement - you should add
else return false;
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
           return(str.equals("0") || str.equals("1") || str.equals("2") ||
               str.equals("3") || str.equals("4") || str.equals("5") ||
               str.equals("6") || str.equals("7") || str.equals("8") ||
               str.equals("9") || str.equals("A") || str.equals("B") ||
               str.equals("C") || str.equals("D") || str.equals("E") ||
               str.equals("F"));

This seems to work. How about simplifying these cmparisions?
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.