Edit method return value in a debugger - java

Given the following Java code, how can I use the IntelliJ or Eclipse debugger to return false from the condition() method?
public boolean condition() {
return (4 == add());
}
public int add() {
return 2 + 2;
}

In Eclipse you can use Force Return.
Just place a breakpoint on return (4 == add()); and type false on the Display view-tab. Then make a selection of your false, right click and hit "Force Return".

In IntelliJ (since IDEA 15 EAP) you can use Force return.
Extract from jetbrains' blog
You can force the return from the current method without executing any more instructions from it:
NB :
If the method returns a value, you’ll have to specify it (smart code completion provided).
If the method has try-finally blocks, you’ll be able to choose whether to execute them or not.

You can first change the code for condition to something like this:
public boolean condition() {
boolean result = (4 == add());
return result;
}
Then, you can set a breakpoint on the return statement. When the breakpoint is hit, you can use the debugger to change the value of result to false.

Related

Java boolean "Unexpected return value"

I'm new to Java and I can't understand why the IDE says that "Unexpected return value" inside the forEach where I declared that the boolean is true or false by an If statement.
My goal is to check that there is an object inside the "States" HashMap which already uses the name that I want to set to a new state. (The HashMap's key is a String which is called IdentifierOfState and the value is my State object which contains variables like its name.) Thank you for your help in advance!
public boolean isStateNameClaimed(String NameOfState)
{
States.forEach((IdentifierOfState, ValueOfState) ->
{
if (ValueOfState.getNameOfState().equalsIgnoreCase(NameOfState)) {return true;}
else {return false;}
});
return false;
}
The problem is that you are attempting to return the results in the wrong place. The {return true;} and {return true;} are in a lambda, so they are attempting to return a result for the lambda. But the inferred type signature for that lambda doesn't allow any values to be returned.
If your intention is that those return statements should be returning a result from isStateNameClaimed, then the better solution is to just use a for loop to iterate the elements of States.
It doesn't help things that your Java code contains a number of egregious Java style violations. You should NOT start the name of a variable with an upper-case letter. It will confuse ... and then annoy ... other people reading your code.
You may say: "Nah, I don't need to follow the rules, 'cos nobody else will be reading my code". But you are asking >>us<< to read your code.
I'm new to Java ...
... so NOW is the time to learn to do it correctly. Java style matters to people reading your code.
This is how I would write it in classic Java:
public boolean isStateNameClaimed(String name) {
for (State v: states.values()) {
if (v.getNameOfState().equalsIgnoreCase(name)) {
return true;
} else {
return false;
}
}
return false;
}
Or using streams:
public boolean isStateNameClaimed(String name) {
return states.values().stream().anyMatch(
(v) -> v.getNameOfState().equalsIgnoreCase(name));
}
Actually ... I just noticed that those two solutions are not equivalent. And based on your description of what you are trying to do, it probably means that the first one and your original attempt are algorithmically incorrect.
forEach will invoke a given callable function for every element. We can't have return value to that function.
Try using "filter" or assign result to local variable.
Return from lambda forEach() in java

When I use reference.removeValue, the boolean variable always returns true

While deleting data from Firebase, I use the remove value function and this function overrides the condition block. My code;
boolean deleteDelayControl=true;
if (deleteDelayControl) {
deleteDelayControl = false;
reference = FirebaseDatabase.getInstance().getReference("taken_foods").child(food_id);
reference.removeValue();
}
As you can see when I run this code, although I set the boolean variable to false, the control block works every time. What could be the problem?
When I delete this function, the control block works properly: "reference.removeValue();"

IntellJ debugger: Can I always apply an expression automatically at a breakpoint?

Is there any way in IntelliJ's debugger to always:
a) set result to true or
b) make foo() return true
I know that I can change the variable when I am in a breakpoint, but I want it to happen always and automatically, without changing the code because it is a decompiled class.
public void foo() {
boolean result = false;
..
return result;
}
Add a breakpoint on the line with return, in breakpoint properties unset "suspend", set "evaluate and log" to result = true.
If there are more than one return in the method, you'll need to modify the result value before all of them.

break from If without loops in java

I want to exit from a function if length of a string( originalPath.length) is equal to some specific number and this is restriction as well I cant do any other thing any idea?
String original = originalPath.substring(0, originalPath.length() - separatorPos);
this snippet is part of a function and not loop.
Make a void method and then
You can do it using if condition and return statement like
if(condition){
return;
}
return is used to stop further execution and return any value back from where it was called.
There are other options like throwing exceptions for the same and break,continue for loops in java
You can use return; if your method is a void, otherwise just return a default value like return false to finish your method if the condition fails.
The return statement will finish your method.

Trying to come up with solution using recursion

I know there are multiple ways to find out if a word is palindromic, like using the reverse function of StringBuilder or even the reverse function of Collections, but in an attempt to learn recursion, I wrote it this way. I even had it working iteratively.
I kind of added return true in my embedded else statement, but I'm really not sure what to do, because when I run this in debug mode, it returns false, then invokes checkPalindrome again, which I don't understand why, because it should return and terminate, no? I would really appreciate an explanation of what I'm doing wrong and how to get it working this way.
public static boolean checkPalindrome(Deque deq) {
if(deq.pollFirst() != deq.pollLast()) {
return false;
} else {
if(deq.size() == 1 || deq.size() == 0) {
return true;
} else {
checkPalindrome(deq);
return true // TODO ?? figure out What to put here ??
}
}
}
It's that you are not returning anything when you call yourself. The inner else statement should read this:
else {
return checkPalindrome(deq);
}
You have a followup question in the comments below that leads me to want to explain how recursive methods work, but in essence, they all follow the following pseudo-code:
public boolean someMethod(T[] someArrayOrList) {
// return true -OR-
// return false -OR-
// call yourself and return whatever that call returns
}
No matter what, when you call the method it will return SOMETHING... Either it will return something itself, or it will return whatever some other call of itself will return. In a way it is AND'ing all the responses, but in reality TRUE is only generated once.

Categories

Resources