0

I just started using Eclipse and I'm quite new to Java. I have a block of code which won't compile because the random number I generated won't cast in to an integer. The error message reads "This method must return a result of type int" on line two in the following block of code.

public class passwordHelper {
    public int rNum(String nums, String lets){


        int z;


        if(nums == "yes" && lets == "yes"){
            z = (int)Math.random()*36;
            return z;

        } else if(nums == "yes"){
            z = (int)Math.random()*10;
            return z;   

        } else if(lets == "yes"){
            z = (int)Math.random()*26 + 10;
            return z;

        } else {
            System.out.println("Sorry, you need either letters or numbers in your password.");
        }
    }
}

As you can see I am using the "(int)" function to cast my number to an integer but it sends me the same error message, I have also tried using other methods of casting, such as "Math.floor()", "Math.round()" and combinations of all the three. And if anyone wants to know this is part of a code to generate a random string of numbers and letters for the user.

//Thanks for any help

Ivar Eriksson
  • 863
  • 1
  • 15
  • 30

5 Answers5

2

You have to return a value in all branches, there are 3 possibilities:

1) Return an int in the else branch:

public int rNum(String nums, String lets){

    int z;

    if(nums == "yes" && lets == "yes"){
        z = (int)Math.random()*36;
        return z;

    } else if(nums == "yes"){
        z = (int)Math.random()*10;
        return z;   

    } else if(lets == "yes"){
        z = (int)Math.random()*26 + 10;
        return z;

    } else {
        System.out.println("Sorry, you need either letters or numbers in your password.");
            return -1;
    }
}

2) Throw an unchecked exception

public int rNum(String nums, String lets){

    int z;

    if(nums == "yes" && lets == "yes"){
        z = (int)Math.random()*36;
        return z;

    } else if(nums == "yes"){
        z = (int)Math.random()*10;
        return z;   

    } else if(lets == "yes"){
        z = (int)Math.random()*26 + 10;
        return z;

    } else {
        System.out.println("Sorry, you need either letters or numbers in your password.");
        throw new RuntimeException("Sorry, you need either letters or numbers in your password.");
    }
}

3) Throw a checked exception

public int rNum(String nums, String lets) throws Exception {

    int z;

    if(nums == "yes" && lets == "yes"){
        z = (int)Math.random()*36;
        return z;

    } else if(nums == "yes"){
        z = (int)Math.random()*10;
        return z;   

    } else if(lets == "yes"){
        z = (int)Math.random()*26 + 10;
        return z;

    } else {
        System.out.println("Sorry, you need either letters or numbers in your password.");
        throw new Exception("Sorry, you need either letters or numbers in your password.");
    }
}
DanM7
  • 2,203
  • 3
  • 28
  • 46
Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
1

your method must return an int value but at your last else block you are not returning anything. return a default value there, for instance return 0 or throw an exception which says invalid input.

And also;

To compare two strings you must use its equals method.

change nums == "yes" to "yes".equals(nums)

and

lets == "yes" to "yes".equals(lets)

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • While definetly true, and will come up later - this is not the question, the comment was enough. – amit Aug 26 '14 at 22:25
  • eagean will not be notified until you add "@name" in your comment. Usually you are always notified for your own question and answer when someone comment you, but not vice-versa. – afzalex Aug 26 '14 at 23:28
  • Great thanks and I actually ended up hitting the second issue you brought up so you sorted that for me as well, can't thank you enough. – Ivar Eriksson Aug 27 '14 at 13:02
1

Try this:

public int rNum(String nums, String lets){

    int z;
    Random random = new Random();

    if(nums.equals("yes") && lets.equals("yes")){
        z = random.nextInt(36);// generates a random number from 0 to 36
        return z;

    } else if(nums.equals("yes")){
        z = random.nextInt(10);//generates a random number from 0 to 10
        return z;   

    } else if(lets.equals("yes")){
        z = random.nextInt(26 + 10);//generates a random number from 0 to 36
        return z;

    } else {
        System.out.println("Sorry, you need either letters or numbers in your password.");
            return -1;
    }
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
0

Math.random() returns a number between 0 and 1. If you cast it to int, you'll obtain 0. You can multiply the double, and then add cast using parantheses.

Alternative: You can use:

Random rand = new Random();

and then

rand.nextInt(...);

P.S.:

  • add a return statement (at the end)
  • change String comparison to use "equals"
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
0

String is a reference type and you cannot compare a reference type with comparative operators. i.e. You cannot use == here.
To compare Strings you can use equals() or equalsIgnoreCase().
equalsIgnoreCase() doesnot consider casing while comparing two strings, and I prefer this to use here. But here I modified your code by replacing == with equals(). If you do not want to consider casing i.e. want true for yes as well as YES here then use equalsIgnoreCase() instead.

int z;
if(nums.equals("yes") && lets.equals("yes")){
    z = (int)Math.random()*36;
    return z;
} else if(nums.equals("yes")){
    z = (int)Math.random()*10;
    return z;   
} else if(lets.equals("yes")){
    z = (int)Math.random()*26 + 10;
    return z;
} else {
    System.out.println("Sorry, you need either letters or numbers in your password.");
}
afzalex
  • 8,598
  • 2
  • 34
  • 61