Win a copy of OCP Java Programmer SE 25 Practice Exams 1Z0-831 this week in the Programmer Certification (OCPJP) forum!
- Post Reply Bookmark Topic Watch Topic
- New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
- Campbell Ritchie
- Tim Cooke
- Paul Clapham
- Devaka Cooray
- paul wheaton
Sheriffs:
Saloon Keepers:
- Tim Holloway
Bartenders:
Help needed with Java program – logic error while checking condition
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am a beginner in Java and currently practicing basic programs.
I am facing an issue with my code logic and I’m not able to understand why it is not working as expected.
Problem statement:
I need to check a condition and return true or false based on the input values.
What I tried:
I wrote the following code, but it throws a runtime error / gives wrong output.
Expected behavior:
If flag is true → return true when either a or b is positive
If flag is false → return true only when both are negative
Actual behavior:
The output is not matching the expected result for some test cases.
I have gone through basic Java condition statements, but I’m still confused about the logic.
Could someone please explain what I am doing wrong and how to fix it?
I am facing an issue with my code logic and I’m not able to understand why it is not working as expected.
Problem statement:
I need to check a condition and return true or false based on the input values.
What I tried:
I wrote the following code, but it throws a runtime error / gives wrong output.
Expected behavior:
If flag is true → return true when either a or b is positive
If flag is false → return true only when both are negative
Actual behavior:
The output is not matching the expected result for some test cases.
I have gone through basic Java condition statements, but I’m still confused about the logic.
Could someone please explain what I am doing wrong and how to fix it?
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What test case is failing? How are you testing?
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
checkStatus() should be static because it doesn't utilize a 'this' object.
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to the Ranch
Please use the code button for code; since you are new I shall go and use the code button on your post, and you can see how much better it looks
I am afraid I don't like the design of that method. Why are you using that boolean? Were you told to? And why have you given it an uninformative name like flag? Can you divide that method into two? Agree with Carey; you should make such methods static.
Please explain what the method is supposed to do.
You have to become very precise about nomenclature; I can see nothing that would “throw a runtime error.” That would mean an exception and nothing in that method can throw an exception. I think the method is written incorrectly and you are getting results different from what you wanted.
Never write == true nor == false which are poor style and very error‑prone. If you write = true nor = false by mistake you will get two errors (or more) for the price of one. Did I say, “if”? I meant, “when”.
Never if (b == true) ... please. Always if (b) ... please.
Never if (b == false) ... please. Always if (!b) ... please.
Please use the code button for code; since you are new I shall go and use the code button on your post, and you can see how much better it looks
I am afraid I don't like the design of that method. Why are you using that boolean? Were you told to? And why have you given it an uninformative name like flag? Can you divide that method into two? Agree with Carey; you should make such methods static.
Please explain what the method is supposed to do.
You have to become very precise about nomenclature; I can see nothing that would “throw a runtime error.” That would mean an exception and nothing in that method can throw an exception. I think the method is written incorrectly and you are getting results different from what you wanted.
Never write == true nor == false which are poor style and very error‑prone. If you write = true nor = false by mistake you will get two errors (or more) for the price of one. Did I say, “if”? I meant, “when”.
Never if (b == true) ... please. Always if (b) ... please.
Never if (b == false) ... please. Always if (!b) ... please.
Campbell Ritchie
Marshal
Posts: 82782
594
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can reduce that method to a single return statement, but you will need to get your brain working hard to get it right.
There is a gap in the logic, which I shall leave to you to find, but that might be intentional.
There is a gap in the logic, which I shall leave to you to find, but that might be intentional.
posted 7 months ago
In the absence of being told anything about the test data, I'm going to guess that when a or b is zero, the expectations of the person doing the testing are incorrect. Or perhaps there's an expectation that the flag values of true and false are going to return different values all the time. Or... of course knowing the details would be more useful than posting hopeful guesses.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Carey Brown wrote:What test case is failing? How are you testing?
In the absence of being told anything about the test data, I'm going to guess that when a or b is zero, the expectations of the person doing the testing are incorrect. Or perhaps there's an expectation that the flag values of true and false are going to return different values all the time. Or... of course knowing the details would be more useful than posting hopeful guesses.
posted 7 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to the Ranch, Nataraj!
One thing to note is that since Java has a built-in boolean data type, expressions like if (x == true) are discouraged, since the logic is redundant and the extra complexity means that it's easy to accidentally code something wrong.
Instead, simple code "if (x) {}". Or for the inverse (!=), code "if (!x)". Ah. I see Campbell also pointed that out.
I could collapse the entire method body to a single statement using Java's trinary operator. However, that's more than you need to worry about right now, and actually, I'm not a big fan of the trinary. It requires less coding
An intermediate simplification would be this:
The "else" is redundant, since there is no other logic to interfere with the alternate return expression and by eliminating it, again, the simpler code is harder to screw up.
As a side note, I used HTML boldface tags in the Javadoc comments. There are more sophisticated ways to highlight those elements, but I was too lazy to refresh my memory. The latest versions of Java allow markdown annotation, but there's also a specific Javadoc tag for that, if memory serves.
Edited: Removed "flag == true"!
One thing to note is that since Java has a built-in boolean data type, expressions like if (x == true) are discouraged, since the logic is redundant and the extra complexity means that it's easy to accidentally code something wrong.
Instead, simple code "if (x) {}". Or for the inverse (!=), code "if (!x)". Ah. I see Campbell also pointed that out.
I could collapse the entire method body to a single statement using Java's trinary operator. However, that's more than you need to worry about right now, and actually, I'm not a big fan of the trinary. It requires less coding
An intermediate simplification would be this:
The "else" is redundant, since there is no other logic to interfere with the alternate return expression and by eliminating it, again, the simpler code is harder to screw up.
As a side note, I used HTML boldface tags in the Javadoc comments. There are more sophisticated ways to highlight those elements, but I was too lazy to refresh my memory. The latest versions of Java allow markdown annotation, but there's also a specific Javadoc tag for that, if memory serves.
Edited: Removed "flag == true"!
The problem with getting rid of the "undesirables" is that sooner or later someone will decide that YOU are an undesirable.
posted 6 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello,
Your logic is actually correct, but the condition can be simplified for better readability. In Java, you do not need to write flag == true. You can simply write if(flag).
Also, your method already returns the correct values based on the condition:
If flag is true, it checks if either a or b is positive.
If flag is false, it checks if both a and b are negative.
A cleaner version of your code would be:
Java
Copy code
class Solution {
boolean checkStatus(int a, int b, boolean flag) {
if (flag) {
return (a > 0 || b > 0);
}
return (a < 0 && b < 0);
}
}
Make sure to test with different values of a, b, and flag to verify the expected behavior
Your logic is actually correct, but the condition can be simplified for better readability. In Java, you do not need to write flag == true. You can simply write if(flag).
Also, your method already returns the correct values based on the condition:
If flag is true, it checks if either a or b is positive.
If flag is false, it checks if both a and b are negative.
A cleaner version of your code would be:
Java
Copy code
class Solution {
boolean checkStatus(int a, int b, boolean flag) {
if (flag) {
return (a > 0 || b > 0);
}
return (a < 0 && b < 0);
}
}
Make sure to test with different values of a, b, and flag to verify the expected behavior
Harini Alagarsamy
Greenhorn
Posts: 6
posted 6 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello,
Your logic is mostly correct. If flag is true, the method checks whether either a or b is positive. If flag is false, it checks whether both a and b are negative.
One small improvement is that in Java you do not need to write flag == true. You can simply use if(flag) because flag itself is already a boolean value.
You should also test your method with different values of a, b, and flag to confirm the results match your expectations.
Your logic is mostly correct. If flag is true, the method checks whether either a or b is positive. If flag is false, it checks whether both a and b are negative.
One small improvement is that in Java you do not need to write flag == true. You can simply use if(flag) because flag itself is already a boolean value.
You should also test your method with different values of a, b, and flag to confirm the results match your expectations.
Campbell Ritchie
Marshal
Posts: 82782
594
posted 6 months ago
You can of course shorten the code considerably with the ?: operator.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That is not a “small” improvement; that is avoiding a potentially serious bug.Harini Alagarsamy wrote:. . . One small improvement . . .
You can of course shorten the code considerably with the ?: operator.
| Don't get me started about those stupid light bulbs. |
-
Bookmark Topic
Watch Topic
- New Topic











