I have been coding for decades and have used many different languages. Currently I am doing a simple Java app for a Uni course. The coursework is easy but I have run into a problem with an if statement that simply doesn't wish to execute even when it matches the condition.
I'm going to admit that I'm a little embarrassed to ask such a simple question but it's a problem that I have spent more time that I care admit trying to resolve.
Allow me to elaborate:
I have two variables, one private local and one generated, both are defined as int. _selectedImage and clickedImageID, both are provided to a public void function that checks if they are the same. The first time the app is run they will be the same, every other time the function is run it is not.
However no matter what I do, it simply doesn't want to execute the if block. Let me demonstrate:
private int _selectedImage;
public void setPuzzlePiece(int clickedImageID) {
// selectedImage == clickedImageID on first run.
Boolean firstRun = (clickedImageID == _selectedImage);
if (firstRun==false) {
// do stuff
String debugPoint = "adding this here just to attach a debug point";
}
}
I have provided a screenshot from the debug process that highlights to the values:
https://i.imgur.com/ahgNZLP.png - you will see the relevant values of 1 and 2, both int, while firstRun is false.
Spot how despite the condition being met the debugger thinks that there is no executable code found at line 77, and stepping through the debugger it just "ignores" the if condition. Yet we can all see the logic, it's quite clear that it should execute the block.
I initially had this as a simple comparison. if (_selectedImage==clickedImageID) { .. } But have shifted the condition to a boolean and compared on that. I have also tried numerous ways of detecting for false such as (firstRun==false), (!firstRun) and (firstRun==(!true)) amongst many other silly ways, but it's as simple as simple can be, just comparing a boolean with either true or false should be enough.
You can also see I feed this into a boolean. I didn't to begin with but as I tried to figure out what is going on I decided to separate out each step (previously I just had if (_selectedImage==clickedImageID) { .. } which wasn't working so I shifted the comparison to a boolean to ensure the comparison was working, and it is, in this case firstRun is false yet asking it to perform the if block when firstRun==false doesn't execute the block.
If I use the evaluation window it returns what the screenshot shows, that the if block condition is met but it still won't bloody execute it.
This could be something to do with Java and it's comparisons. As I use multiple languages I can sometimes get caught out with the foibles of each one; for instance in c# you can compare strings via ==, while in Java you use equals().
It HAS to be something I am doing wrong, but for the life of me I cannot see it. Help me spot my stupidity SO!
- ps: I checked to see if this was a duplicate question and while a few threads relate to similar, they are in PHP or C++, not Java, and of the Java one it's a simple syntax error that failed them. This is, from what I can see, not the issue here. *
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire