I can't seem to find a way to fix this problem. All i'm doing is declaring an integer and it's telling me that the code is unreachable.
private class myStack{
Object [] myStack = new Object[50];
private void push(Object a){
int count = 50;
while(count>0){
myStack[count]=myStack[count-1];
count--;
}
myStack[0]=a;
}
private Object pop(){
return myStack[0];
int count2 = 0; //Unreachable Code
}
}
Once you return from a method, you return to the method that called the method in the first place. Any statements you place after a return would be meaningless, as that is code that you can't reach without seriously violating the program counter (may not be possible in Java).
Quoting a comment on the question by Jim H.:
You returned from the pop() method. Anything after that is unreachable.
Unreachable code results in compiler error in Java.
In your program the line
int count2 = 0;
will never be reached since it is after the return statement.
Place this line above the return statement to work.
The simple explanation in plain English would be the following:
private Object pop(){
return myStack[0];
int count2 = 0; //Unreachable Code
}
method private Object pop(){} is looking for a return type Object and you just gave that return type by writing return myStack[0]; .So your method does not necessarily reach int count2 = 0; because it assumed that the method already reached its goal.
The last statement in the function should be a return statement.
Linting tools would flag it since it messes with the allocation of new memory after the counter scope ends.
Declare before return myStack[0] that fixes
Related
I am writing a method that is part of a Queue class for a college assignment. It is supposed to search a linked list for an object and if it is found return the index of the object, and if it isn't then it returns -1. When I don't have the "return -1;" statement in my code below I get an error saying that I don't have a return statement when I clearly have one above in the for loop. What is the issue here?
public int find(Object item) {
Node current = head;
for(int index = 0; index < size; index++) {
if(current.data.equals(item)) {
return index;
}
else {
current = current.next;
}
}
return -1;
}
It's not clear where size is defined, but let's say that index < size is never true, then your return statement is never seen
Therefore, the method scope needs a final return statement for all possible code paths
Because during compile time compiler doesn't know the value of the variables. So compiler has no way to evaluate the conditions and check if a condition is true or false. Therefore eliminating the "return -1" statement giving the error. Hope this clears your confusion.
below the code is using a private method to add to the variable count. Below that variable are conditionals which by my understanding, will not run until the recursion stack traces upword. Am I correct? My test is failing, and I am trying to see if it is because my code is wrong or I'm using recursion wrong.
public boolean containsRightRedEdge() {
int count = 0;
count += containsRightRedEdge(root);
if(count > 0) return true;
return false;
}
private int containsRightRedEdge(Node n) {
if (n == null) return 0;
if (isRed(n.right)) {
return 1;
}
return containsRightRedEdge(n.left) + 0 + containsRightRedEdge(n.right);
}
I would say you are using recursion pretty much correctly, but your choice of method names could be less confusing, and your logic could be simplified.
I am not too familiar with the algorithm you're trying to implement, but you might try something like this:
public boolean containsRightRedEdge(Node root) {
return getNumRightRedEdges(root) > 0;
}
private int getNumRightRedEdges(Node n) {
if (n == null) return 0;
if (isRedEdge(n)) return 1;
return getNumRightRedEdges(n.left) + getNumRightRedEdges(n.right);
}
Generally a recursive method shouldn't have the same name as a non-recursive method. These method names communicate more clearly what each one does. Also your base cases might be wrong as you've got them written currently based on how I'm interpreting the algo should work. Of course, I don't know the code inside isRed() so I'm probably making wrong assumptions here.
The code above in my question, is the correct way to use recursion in this instance. I just had a typo which is now resolved. Leaving the question for other peoples reference.
public static int someFunc(int a, int b){
while(a <=b){
a+= 1;
return a;
}
return b;
}
so i was expecting it to return the new value over and over but it didnt, once i executed the code and saw for myself that, i realised it had something to do with pass by value or by reference which is something i dont really understand! can someone explain?
The immediate problem is that return returns! Nothing after it can be executed. You can't have a meaningful loop with an unconditional return in it.
As far as the other, no. That's not the issue as you return the new value of a. The a you passed in remains unchanged which is the pass by reference/value you speak of. Java is pass by value.
public class JHelp {
public static void main(String...args) {
JHelp j = new JHelp();
int a = 1;
System.out.print(j.f(a));
System.out.print(a);
}
int f(int a ) {
a += 1;
return a;
}
}
Will give you an output of:
21
because the return instruction exits the code hence the methods done its job it doenst need to iterate again once it reaches the return instruction, i however would have done it if the return instruction wasnt reached.
I am a beginner so please bear with me. I decompiled the source code of a professional application. When I copied the source code into eclipse, an error came up with the following code( the error is with the return type):
public boolean method(){
...
...
for(int i = 0; ; i = 1){
return i;
}
How can I change the code to keep it correct but keep the functionality?
You can cast i as a boolean, or change return i; to return i != 0;, which will be true for all non-zero values of i, and false if i == 0.
As a side note, I really see no reason to wrap a return in a for loop. In this case, you might as well just replace both of those lines with return 0; (or return false;, to match the method signature).
Change the return type from boolean to int.
As you can see, your method is returning i which is declared as an int in the for loop, so the return type of your method has to be the same as the type of the variable it returns.
I have these two methods and Java can't find the "return" in getNumEmails(). All of them are in the same class, which only has static methods
private static int posSymbol=0;
private static int cont=0;
private static String text=null;
private static int getPosSymbol(){
posSymbol=text.indexOf('#',posSymbol);//if there is no symbol, returns -1
return posSymbol;
}
//Main calls this method
public static int getNumEmails(String completeText){
text=completeText;
while(posSymbol!=(-1)){
posSymbol=getPosSymbol();
if(posSymbol!=(-1)){
cont++;
posSymbol++;
}//close if
else{
return cont; //It seems that it doesn't reach the return always
}//close else
}//close while
}//close method
I know that the solution is simple, to delete the "else" and put the return cont; after the while. But I want to know why Java thinks that getNumEmails() could end without returning anything.
I suppose this is about the compiler complaining This method must return a result of type int.
While compilers can sometimes determine if a function will reach the return statement, this is not always the case. It is mathematically impossible to statically determine the dynamic behaviour of a program.
This is called the "Halting Problem" in computer science; it is impossible to determine, in the general case if a program will or will not terminate.
So even though you can determine that the method will always reach one of your return statements, the compiler may not be able to do so.
It can find the return in the else clause. The problem is you need to have a return for all execution paths, even the one when posSymbol is equal to -1, since when posSymbol is equal to -1 you never enter the while loop.
Hence you need a return after the while statement.
Simply add a return 0; after the "//close while".
Not an answer, but here's a much safer version of the method with no static variables.
//Main calls this method
public static int getNumEmails(String completeText)
{
int posSymbol=0, count=0;
while(posSymbol!=(-1))
{
posSymbol=completeText.indexOf('#',posSymbol); //if there is no symbol, returns -1
if(posSymbol!=(-1))
{
++count;
++posSymbol;
}//close if
}//close while
return count;
}//close method
The Java compiler is not able statically (at compile-time) verify that the while loop gets executed at all (posSymbol could be -1 upon the first invocation). Hence the error.
In general it's not a good idea to keep looping state in member variables. What if more than one Thread executes your method (e.g. in a Webserver)? In the long run it's easier to track down bugs if your code does not modify global state:
public static int getNumEmails(String completeText) {
int count = 0;
Matcher m = Pattern.compile("#").matcher(completeText);
while (m.find()) {
count++;
}
return count;
}