I'm trying to increase numberOfQuestion by 1 at any time I call isOdd() method as shown below. I got an error said "
unreachable code
". how I could do it right?!
import java.util.Scanner;
public class Geek {
private String geekName;
private int numberOfQuestion;
public Geek (String name) {
geekName=name;
numberOfQuestion=0;
}
public int getNumberOfQuestion() {
return numberOfQuestion;
}
public boolean isOdd(int num) {
if(num%2==0) {
return false;
}
else {
return true;
}
numberOfQuestion++;
}
}
Put numberOfQuestions++ as the first line in method public boolean isOdd(intNum)
The message is correct, your line is unreachable since you return from method before incrementing it.
numberOfQuestion++; is considered unreacheable code because in either case:
if num is odd, your method returns true and exit the method.
if num is even, your method returns false and exit the method as well.
As you can see there is no way it will be able to reach the line:
numberOfQuestion++;
Hence, if you want to do something, you need to do it before your method ends:
public boolean isOdd(int num) {
//whatever you want to do here.. such as numberOfQuestion++;
return (num % 2 != 0);
}
put the increace statement at first line:
public boolean isOdd(int num) {
numberOfQuestion++;
if(num%2==0) {
return false;
}
return true;
}
At last line it is unreachable, because you return in if and in else
After return method closed and numberOfQuestion++ never done.
You need this:
public boolean isOdd(int num) {
numberOfQuestion++;
if(num%2==0) {
return false;
}
else {
return true;
}
}
It is because in function 'isOdd', whatever the 'num' may be, the function get returned and the last line,
numberOfQuestion++;
will never execute, So it's an unreachable code.
just make the code like this,
public boolean isOdd(int num) {
numberOfQuestion++;
if(num%2==0) {
return false;
}
else {
return true;
}
}
Related
I'm trying to find out if a number is a prime number or not. I created this method, which I will be using in another class later.
When compiling it tells me that I need a return statement outside the for-loop, but if I try to return the boolean value it gives me an error (cannot find symbol). What shall I return?
public class NumeroPrimo {
public static boolean primo(int numero){
for (int i=2; i<numero/2; i++){
if(numero%i==0){
return false;
}
else return true;
}
}
}
If the loop was not done (numero 1), no return would happen.
Also you return true too often.
public static boolean primo(int numero) {
for (int i = 2; i <= numero/2; i++) {
if (numero % i == 0){
return false;
}
}
return true;
}
Also for 4 <= is required.
I am posting here two functions. In the findPrime(int m, int i, int n) I have one outer if and outer else block from both the block I am getting return so it can be considered that the function is returning something I put same structure in endOther(String a, String b) i.e.
there are two main if-else blocks both are returning value as per function's return type so we can say that function is returning something but the endOther(String a, String b)
function is throwing compile time error saying that put return statement while first function is not throwing such error. I am not understanding this issue please help me. Type both the functions in eclipse and check
1.
static boolean findPrime(int m, int i, int n){
if(n == 0 || n == 1)
return false;
else {
if(i <= m) {
if(n%i == 0)
//if(m%i == 0)
return false;
else {
i++;
//return findPrime(m,i);
return findPrime(m,i,n);
}
}
else {
return true;
}
}
}
2.
public boolean endOther(String a, String b) {
if(a.length()==b.length()) {
if(a.equals(b))
return true;
else
return false;
}
else {
if(a.length()>b.length()) {
if(a.substring(a.length()-b.length(),b.length()).equals(b))
return true;
}
else {
if(b.substring(b.length()-a.length(),a.length()).equals(a))
return true;
else
return false;
}
}
}
Your endOther function must return a value on all possible execution paths. In case a.length() > b.length() the return may not be executed based on the condition of the inner if.
public boolean endOther(String a, String b) {
if(a.length()==b.length()) {
if(a.equals(b))
return true;
else
return false;
}
else {
if(a.length()>b.length()) {
// IF THIS FAILS THERE IS NO RETURN!
if(a.substring(a.length()-b.length(),b.length()).equals(b))
return true;
}
else {
if(b.substring(b.length()-a.length(),a.length()).equals(a))
return true;
else
return false;
}
}
}
As a general note, you could use some of the methods in String to improve your code, e.g. String.endsWith instead of the substring operation. This would be more readable. a and b being identical is a special case of String.endsWith, so the following should be equivalent:
public boolean endOther(String a, String b) {
return a.endsWith(b) || b.endsWith(a);
}
I am getting output as false, everytime.
My aim is to print true, if String t is present in the same order in String s.
For example:
String s = "gagbgcgd";
String t = "abcd";
Expected output:
true
String s = "gagcgdgb";
String t = "abcd";
Expected output:
false
Here is the code.
public class StringCompare {
public static boolean stringCompare(String t,String s) {
if (t.length() == 0) {
return true;
}
if (s.length() == 0) {
return false;
}
if (t.charAt(0) == s.charAt(0)) {
stringCompare(t.substring(1), s.substring(1));
}
else {
stringCompare(t, s.substring(1));
}
return false;
}
public static void main(String[] args) {
String s = "acaoadaianaga";
String t = "coding";
System.out.println(stringCompare(t,s));
}
}
When you recurse, you don't return the result of the recursion. Change
if(t.charAt(0)==s.charAt(0)){
stringCompare(t.substring(1), s.substring(1));
}
else{
stringCompare(t, s.substring(1));
}
to something like
if(t.charAt(0)==s.charAt(0)){
return stringCompare(t.substring(1), s.substring(1));
}
else{
return stringCompare(t, s.substring(1));
}
The main problem of your code is in the first execution of the recursion always return false no matter what the return value of remaining execution in the recursion.
You should change your code to something like:
if(t.charAt(0)==s.charAt(0)){
return stringCompare(t.substring(1), s.substring(1));
}
else{
return stringCompare(t,s.substring(1));
}
and remove the last return false; statement.
This is because the outer recursive calls you always returns false
except
if(t.length()==0){return true;}
look at Elliott Frisch's answer.
You should use .contains. Example:
boolean istrue = t.contains(s);
I have a scenario where an object receiving from UI passes a Boolean value and this is stored in database as an Integer like TRUE=1 and FALSE=0. Now when the flag is changed for ex to FALSE i need to compare its integer value if its 0 then do nothing if 1 then change to 0 and update. The one way is below, but still there maybe a better way to do this.
class AClient {
static Boolean x;
}
class BServer {
static Integer y;
}
public class Demo {
public static void main(String[] args) {
AClient.x = Boolean.TRUE;
BServer.y = 0;
System.out.println(storedValues());
}
private static Boolean storedValues() {
if (AClient.x) {
if (BServer.y.equals(new Integer(1))) {
return true;
} else {
return false;
}
} else {
if (BServer.y.equals(new Integer(1))) {
return false;
} else {
return true;
}
}
}
}
Output: false
Your storedValues method can be reduced to:
return AClient.x.equals(BServer.y.equals(1));
If x and y don't need to ever be null, I would replace them with primitives instead of the wrapper class, then storedValues could look like this:
return BServer.y == 1 == AClient.x;
You could also change the return type of storedValues to boolean.
There's a shorter way to write it:
Instead of:
if (BServer.y.equals(new Integer(1))) {
return true;
} else {
return false;
}
write :
return (BServer.y == 1) ? true : false;
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX) {
return true;
}
else {
return false;
}
}
Can the code above be written without the else statement and just have return false; after the if statement and still have the same result? If so, why? This is what I mean
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX) {
return true;
}
return false;
}
You should just write
public boolean percentDepreciatedOutOfRange() {
return percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX);
}
which is more readable. Avoid statements like
If (xxx) { return true; }
else { return false; }
because the if just adds noise around the expression.
The reason why this is legal is that the boolean type is the same in evaluating the if expression and in the return type of the method.
Because return actually exits the function (method).
In the case the if is not entered the rest of the code will execute normally.
Since you have boolean returns, having return false at the end of the function, means everything up to the end failed.
You could even rewrite it to:
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN) {
return true;
}
if (percentDepreciated > DEPRECIATION_MAX) {
return true;
}
return false;
}
To clearly state the tests in the method.
Another way I like:
Defining return variable as 1st statement with default value.
Change the variable in method in various biz logic
Return the variable value (Single return statement)
public boolean percentDepreciatedOutOfRange() {
boolean status = false;
status = percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX;
return status;
}