-1

Okay so this program is supposed to use a user defined method to find the numerical value of each character of a string and assign it to a number on a keypad... i.e. 1800FLOWERS to 18003569377

When i try to compile im getting a "missing return statement" at the very end of the getNumber method. Could someone help me out please

import java.util.Scanner;           //import scanner object

public class phone

    {
    public static void main(String [] args)
    {
    int i = 0;
        Scanner scan= new Scanner(System.in);           



        System.out.println("Enter a phone number"); 
        String number = scan.nextLine();
        String upperCaseLetter = number.toUpperCase();
        int length = number.length();   

        if (i <= length)
            {   
                char result = number.charAt(i);
                int carat = getNumber(result);
                System.out.print(""+carat+"");
                i++;
            }

        }



        public static int getNumber(char result) 
        {
                if (result == 65 || result == 66 || result == 67)
            {
                result = 2;
                return result;
            }   
                else if (result == 68 || result == 69 || result == 70)
            {
                result = 3;
                return result;
            }   
            else if (result == 71 || result == 72 || result == 73)  
            {
                result = 4;
                return result;
            }

            else if (result == 74 || result == 75 || result == 76)  
            {
                result = 5;
                return result;
            }
            else if (result == 77 || result == 78 || result == 79)  
            {
                result = 6;
                return result;
            }
            else if (result == 80 || result == 81 || result == 82 || result == 83)  
            {
                result = 7;
                return result;
            }
            else if (result == 84 || result == 85 || result == 86)  
            {
                result = 8;
                return result;
            }
        else if (result == 87 || result == 88 || result == 89 || result == 90)  
            {
                result = 9;
                return result;
            }

        }   
    }
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Awesome, well i added a "return -1" at the end of my method but now I'm just getting -1 as an output... At least now it's just a logic error i suppose HOORAY! – user2961522 Nov 06 '13 at 17:31
  • Okay so I made some changes to account for regular numbers being entered, But I'm still getting a -1 return – user2961522 Nov 06 '13 at 17:37
  • As I noted in my comment below, you only check charAt(0) which is a single char and not an int. To check if your char is for ex. 0, check `if(result == '0')`. – 3yakuya Nov 06 '13 at 17:41
  • Remember to accept an answer that you found most helpful. – 3yakuya Nov 06 '13 at 18:11
  • Also by the way, removing the text of the question after it's been answered is probably not particularly useful. – Radiodef Nov 06 '13 at 18:48

3 Answers3

1

You are returning from a condition (in your method getNumber), May be one of your condition is satisfied at run time but the compiler is not smart enough to determine that, Hence the error.

At the end your method you can have return 0 or return -1 for returning invalid result

Habib
  • 219,104
  • 29
  • 407
  • 436
  • If I have a branch that should be unreachable, I usually prefer using `throw new RuntimeException("UNREACHABLE");` as opposed to something like `return -1;`. – DaoWen Nov 06 '13 at 17:21
  • I'm not quite sure what you mean. Are you saying i need to include a "default" statement and if so, where exactly. This is for my first java class so Im pretty new to this. – user2961522 Nov 06 '13 at 17:21
  • @user2961522, you could throw an exception like `@DaoWen` said, or just place `return - 1` before your method ends. – Habib Nov 06 '13 at 17:23
0

Thats because you have if and else if blocks with return statements but no else block or return statement at the end. Note the return statement at the second last line.

public static int getNumber(char result) 
{
        if (result == 65 || result == 66 || result == 67)
    {
        result = 2;
        return result;
    }   
        else if (result == 68 || result == 69 || result == 70)
    {
        result = 3;
        return result;
    }   
    else if (result == 71 || result == 72 || result == 73)  
    {
        result = 4;
        return result;
    }

    else if (result == 74 || result == 75 || result == 76)  
    {
        result = 5;
        return result;
    }
    else if (result == 77 || result == 78 || result == 79)  
    {
        result = 6;
        return result;
    }
    else if (result == 80 || result == 81 || result == 82 || result == 83)  
    {
        result = 7;
        return result;
    }
    else if (result == 84 || result == 85 || result == 86)  
    {
        result = 8;
        return result;
    }
else if (result == 87 || result == 88 || result == 89 || result == 90)  
    {
        result = 9;
        return result;
    }
    return -1; // NOTICE THIS
}   
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
0

Your method getNumber need to cover all cases, also those where result is none of those you've mentioned in your code (otherwise there are some cases in which the method will not return anything, and that is what your compiler is angry about).

For you it does not change much: since you are sure your result is from a specified range, you can for ex. add return 0; (and not return null, as null is not an int, and your method is declared to return an int).

EDIT: your method returns -1 as your charAt(0) is a single char, and not an int. Check whether charAt(0) == '0' and not charAt(0) == 0, as the second compares your char to an int. You need to know that 1 != '1'. What is more, your if (i <= length) just checks charAt(0) and nothing more, as you are not looping it. If you want to go through the whole string number, go for while(i <= length) instead of an if.

3yakuya
  • 2,622
  • 4
  • 25
  • 40
  • I see what you mean. I was trying to compare the charAt(i) to the hexadecimal value of the character at i. I made the necessary changes i.e. if (result == '1') { result = 1; return result; } it worked i think but my loop stopped so i only got the first character read out. – user2961522 Nov 06 '13 at 17:51
  • `If` is not a loop, and i++ at the end of it does not change it. To loop through the characters of a string this way, you need to use `while` instead of `if`. – 3yakuya Nov 06 '13 at 18:08