user2340601
user2340601

Reputation: 45

Errors with missing return statement

Every time i try to compile my code i get a error about a missing return statement. Any ideas about whats wrong with my code?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx So i fixed a couple of things, but now im getting an error where my variable 'result' might not have been initialized, any suggestions?

    import javax.swing.JOptionPane;
    import java.io.*;
    public class facts
    {
      public static void main(String[]args)
    {

    String input;
    int x;
    char y,prime,perfect;
            do{
        input = JOptionPane.showInputDialog("Enter an integer");
        x = Integer.parseInt(input);
        if(x%2==0)
                System.out.println("The integer is even - it is evenly divisible by 2");
            else
                System.out.println("The integer is not even - it is not evenly divisible by 2");
            prime = isPrime(x);     
            if(prime == 't')
                System.out.println("The integer is a prime number");
            else
                System.out.println("The integer is not a prime number");
            perfect = isPerfect(x);
            if(perfect == 't')
                System.out.println("The integer is a perfect number");
            else
                System.out.println("The integer is not a perfect number");

        input = JOptionPane.showInputDialog("Enter Y for another number, anything else to quit");
        y = input.charAt(0);
        }while(y=='Y');
            System.out.println("Good Bye");

            System.exit(0);
}            
    public static char isPrime(int x)
    {
        for(int y=2;y<x;y++)
        {
            if(x%y==0)
                return 't';
            else 
                return 'f';
        }
    }
    public static char isPerfect(int x)


 public static int triAng(int x)
{
    int result,z,y = 1;
    while(y<=x)
    {
        z=y*(y+1)/2;
        y++;
        System.out.println(z);
        result = z;
    }
    return result;
}

Upvotes: 0

Views: 124

Answers (4)

Daniel Pereira
Daniel Pereira

Reputation: 2785

In the isPrime and isPerfect methods, your code may not enter in the for loop. To adjust it, put a default return on the end of these methods or throw an exception.

Upvotes: 0

talnicolas
talnicolas

Reputation: 14053

You need to put return statements after your for loops and return a default char or null, in case the loops would not be entered. And in your isPerfect, even the if may not be entered.

For your variable 'result' might not have been initialized problem, the problem is that line:

int result,z,y = 1;

only the y variable is initialized to 1. As you might not enter the while loop, then the return statement would return result with it not having been initialized, so you need to explicitly specify a value to it (null or whatever integer). If you want them all to be 1 you can do:

int result,z,y;
result = z = y = 1;

Upvotes: 2

Lee Meador
Lee Meador

Reputation: 12985

For example, this code might not return if x <= 1 or if x%y is never 0:

 public static char isPerfect(int x)
    {
        int y,z=0;
        for(y=1;y<x;y++)
        {
            if(x%y==0)
            {
                z+=y;
                if(z==x)
                    return 't';
                else
                    return 'f';
            }
        }
    }

Upvotes: 0

Jake Smith
Jake Smith

Reputation: 2803

in your isPerfect method, you don't have a return statement for the case that the code does not enter if if(x%y==0) block.

Upvotes: 0

Related Questions