Eclipse - Java, Casting from Double to Int
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...
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
5 Answers
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.");
}
}
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)
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;
}
}
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"
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.");
}