-4

I really can't figure out why my code is causing this error, everything looks correct, thought it keeps coming up as it is missing a return statement }

I tried looking for solutions, and I saw that "while" after "if" is one solution, but since I need multiple numbers I cannot use while, and have to go with "what if"

Could anyone help me out?

import java.util.*;

class WS8Q4 
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);

        int x = 0;

        System.out.println("Please put in an integer from 0 - 9");
        x = in.nextInt ();

        String answer = numTxt (x);
        System.out.println(answer);
}

public static String numTxt (int x)
    {
        if (x==0)
        {
            return ("Zero");
        }
        else if (x==1)
        {
            return ("One");
        }
        else if (x==2)
        {
            return ("Two");
        }
        else if (x==3)
        {
            return ("Three");
        }
        else if (x==4)
        {
            return ("Four");
        }
        else if (x==5)
        {
            return ("Five");
        }
        else if (x==6)
        {
            return ("Six");
        }
        else if (x==7)
        {
            return ("Seven");
        }
        else if (x==8)
        {
            return ("Eight");
        }
        else if (x==9)
        {
            return ("Nine");
        }
    }
} 
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user3062391
  • 45
  • 2
  • 6

6 Answers6

5

What if x is something besides 0-9? You don't have a return statement for that case. Add it at the bottom, below your last else if:

return "Other";
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • my god that fixed it, thanks ! didn't even notice that, was 99% sure that my code was already correct, but I guess computers never lie ! – user3062391 Dec 20 '13 at 18:07
1

You need to have a default return statement.

What if , no condition satisfied ?? add an else on the end.

else{
  return "not found";
}

And you should write

return "Zero";

There is no need to write return ("Zero");

And your case is perfectly suits for switch case, If you are using >1.6

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You need a return in the ending of public static String numTxt() method, otherwise what happen if none of your if block satisfy?

Paul Lo
  • 6,032
  • 6
  • 31
  • 36
0

Add an else with a return statement. If x isn't 0-9, then it doesn't hit any of those return statements. That is what is causing the issue.

forgivenson
  • 4,394
  • 2
  • 19
  • 28
0

You don't have a return statement at the end of the function. All of your return statements are within if branches; the compiler can't be certain that any of them will ever be hit. You either need a return at the very end, or one inside an else branch.

dg99
  • 5,456
  • 3
  • 37
  • 49
0

You should take care of all cases, ie: all the other cases you described in your if-else chain. To fix it, I would throw an exception, indicating that the digit is impossible:

public static String numTxt (int x)
{
    String txt[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
                    "Seven", "Eight", "Nine"};
    if (0 <= x && x <= 9) return txt[x];
    throw new IllegalArgumentException("Unsupported digit!");
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287