Basically, this peice of code seems to behaving differently when I move the breakpoints
int checker = something.length(); /* something is the value of an edittext */
boolean badInput = false;
if(checker == 0)
{
badInput = true;
}
if(checker > 12)
{
badInput = true;
}
*1 if(badInput = false)
{
*2 /* A lot of
code to do
if the
input is GOOD */
}
else
{
/* Alert that the input is BAD */
}
When I enter a 2 digit number into the edittext with the first breakpoint (1), badInput gives false, as it should.
Here is the problem: when I do exactly the same with only the second breakpoint (2), the code goes onto the else statement, and alerts, even though the input is exactly the same.
Anybody know why this might be?
This:
if(badInput = false)
Should be:
if(badInput == false)
Or preferrably:
if (!badInput)
The first is performing an assignment, not a comparison. The overall result of the expression badInput = false is also the value assigned (false) so it will never enter the body of that if.
It's not really clear what you mean by entering data "with" a breakpoint, but fundamentally the problem is in your code.
You should have:
if(!badInput)
or
if(badInput==false)
(the first one is better)
Simple typo error,
if(badInput = false)
should be
if(badInput == false)
you want a compare not assign.
What you check boolean variable with == operator ?!!
You have to check as following :
if(!badInput) //for false value
or
if(badInput) //for true value
Related
I have the following nice one liner :
boolean outcome = count > 0 ? false : true;
But from sonaqube I get 'Remove the literal "false" boolean value'
The solution seems to assume you can re-write as a function
But even that function will have that simple one liner and put me in the same position, I don't quite understand how to fix ? Ideas ?
The issue is that you are doing extra gymnastics on an operation that already produces a boolean.
If I write out what you have coded in full syntax:
boolean outcome;
if(count > 0){
outcome = false;
} else {
outcome = true;
}
essentially, you are reversing the count > 0
So try
boolean outcome = !(count > 0)
or even better
boolean outcome = count <= 0
For reference to others I was being dumb and SonarQube correct in reporting the above as it's doing more than needed. Correct code is:
boolean outcome = count > 0;
you do not need the ternary statement the result of count > 0 is a true/false value...
So as stated above to reverse that outcome is simple
boolean outcome = !(count > 0);
I have a problem where .contains() is not returning true when looking for a substring.
See the below code snippet and image which shows variable values for the following code. The problem I have is that the expression returns false, when I think it should obviously be true.
if (this.fileName.toLowerCase().contains(extension.toLowerCase())){
return true;
}
I have encountered this today, after searching the web I can't see anything obvious that is going on here.
I have tried the same using literals, but still does not evaluate to true:
if ("Android App Image.jpg".toLowerCase().contains("jpg".toLowerCase())){
return true;
}
I have broken it down further so the result is in a boolean:
boolean result = ("Android App Image.jpg".contains("jpg"));
if (result) {
return true;
}
The result is true, so .contains is not the issue, rather it appears my if() doesn't evaluate the boolean as true and doesn't cause return true to execute.
I now think it's an issue with my while loop (full function below). Does the enumeration run asynchronously?
public boolean isImage(){
String[] imageExtensions = {"jpg", "png", "bmp", "gif"};
for (String extension: imageExtensions) {
if (this.localFileURLString != null && this.localFileURLString.toLowerCase().contains(extension)){
return true;
}
boolean result = ("Android App Image.jpg".toLowerCase().contains("jpg".toLowerCase()));
if (result) {
return true;
}else{
Log.i("TEACH", "If evaluated false");
}
}
return false;
}
UPDATE 1/2:
I have trimmed down the entire function, with hard coded literals and no longer using fast enumeration, which still returns false when I expect it to return true:
public boolean isImage(){
String[] imageExtensions = {"jpg", "png", "bmp", "gif"};
for (int x = 0; x < imageExtensions.length; x++) {
// String extension = imageExtensions[x];
boolean result = ("Android App Image.jpg".toLowerCase().contains("jpg".toLowerCase()));
if (result) {
return true;
}else{
Log.i("TEACH", "If evaluated false");
}
}
return false;
}
The function does not returns true or does the "If evaluated false" Log get executed, it just jumps straight to the last line of the function (return false;) after evaluating the line if (result) {... it's as if the if breaks the loop.
UPDATE 2/2:
Quite simply, even the most simple of if statements inside my function would cause the code to jump to the last line of the function and return false. For example:
if(true == true){
return true
}
This happened when stepping through in debug mode and also coded logs show the same result when not attached to a bugger.
After returning from the function every carries on as normal. No errors or warnings.
re-installing Android Studio, trying different simulators and physical Android devices didn't resolve the issue.
Renaming the function didn't resolve the issue.
Changing the return type of the function from boolean to Boolean did resolve the issue.
I'm well aware of the differences in these types, but I'm still perplexed as to to why this would this this problem. I can only assume changing the method signature in this way solved is something funky was happening at compile time.
I'll leave the question open for now, as even though changing the return type "fixed" the issue for me, i'm still not satisfied i'll know why... probably never will.
Hopefully somebody much more knowledgable on these sorts of issues can enlighten is all!
Why don't you try using:
if (this.fileName.toLowerCase().contains(extension.toLowerCase()==true){
}
Your search term produces false positive . Cause the file name string can have a substring that matches the extension.
To avoid this you can prepend the search terms by a dot.
String[] imageExtensions = {".jpg", ".png", ".bmp", ".gif"};
This will apperantly solve your problem .
It can produce false positive if there's any irregular use of DOT in the file name.
Hope it helps.
I've been writing a program for my uni course and I'm stumped on something that is probably ridiculous.
Here's a bit of my code
// International Calls Selection
System.out.println("Would you like to include international calls?");
System.out.println("1. Include");
System.out.println("2. Do not Include");
int intCallChoice = keyboard.nextInt();
boolean intChoice = true;
if (intCallChoice == 2)
{
intChoice = false;
}
This part takes the input from the user as you can see, then in my contract class I have a price calculator method which goes like this:
if (intChoice = true)
{
priceOfContract *= 1.15;
}
However, the result always ends up being a 15% increase, no matter if I select to include International Calls or to not include them?
if (intChoice = true)
will always be true because you are using assignment(single =) and not equals(double ==)
Your condition should be
if (intChoice == true)
or simply
if (intChoice) // for boolean check
Try to use == instead of = like this:
if (intChoice == true){
The = it is used for assign, but == it's used to check equality.
Or use just:
if (intChoice){
As others have said you need to use == to compare values, however since your variable is a boolean you can shorten this if statement even further:
if (intChoice) {
priceOfContract *= 1.15;
}
if (intChoice = true)
Is a condition, and when you want to compare the intChoice variable value with true, you must use two equal signs (==), the same way you did in:
if (intCallChoice == 2)
If you use only one equal sign you are changing the value of the intChoice variable to true and the condition will be allways
if(true)
Which means for any type of calls the priceOfContract will be multiplied by 1.15.
I'm new to Java as of the past day or two and I've been messing around making some very basic programs. I have the following code as a simple number guessing program
public class GUESS2 {
public static void main(String args[])
throws java.io.IOException {
//This program will have you solve a numeric puzzle using different hints as you get closer
int answer='5', guess=0, i=0;
char ignore;
do{
i++;
System.out.print("Can you guess the number I'm thinking of???");
guess = (int) System.in.read();
if(guess==answer) System.out.println("CORRECT!!!");
if(guess >answer) System.out.println("Too high!");
if(guess <answer) System.out.println("Too low!");
do{
ignore=(char) System.in.read();
}while (ignore != '\n');
} while(answer!=guess && i != 5);
}
}
My issue here is with the "while" statment. I want it to exit this loop when EITHER answer=guess or i=5. When I use | or || it gives me the opposite and ONLY exits when I do answer=guess and i=5 at the same time which seems backwards to me.This current code using "&&" works when EITHER answer=guess or i=5 which doesn't make sense to me.
Again I'm a beginner and I appreciate you guys taking the time to help me- probably something simple but I've messed with it for a while.
Let's consider the possibilities. First, with OR(||) operator:
(answer != guess || i != 5)
If answer is equal to the guess, answer != guess evaluates to false, which, based in what you described, should make the program exit. However, i != 5 will still be true, so the whole statement will evaluate to true, making the program keep going, while the statement is true.
If answer is equal to the guess, and i == 5, both will evaluate to false, and, as none of them are true, the OR will evaluate to false, making the loop exit.
Now, with AND(&&) operator:
(answer != guess && i != 5)
If answer is equal to the guess, answer != guess evaluates to false, and, as it is an AND operator, which requires both to be true, the statement will evaluate to false, making the loop exit. If i is equal to 5, i != 5 evaluates to false, and, as already said, will make the statement evaluate to false, and the loop exit.
Hope to have helped.
EDIT: A link I found, may help you, see the 8th item.
If you want to exit the loop when either A or B is true, then you want to stay as long as this isn't the case: when both A and B are false. Which is just how your code reads.
The && gives you the behaviour you want because it requires both of those conditions to evaluate to true in order to continue. This means that if answer equals guess (the first conditions fails), or i equals five (the second condition fails), the loop will exit. However, with the || operator, you're saying continue on as long as either one of these is true. They're known as logical and and logical or, respectively. This should help:
a && b -> if both are true, evaluates to true, otherwise false
a || b -> if either are true, evaluates to true, otherwise false
it does not make sense because you did not read the things in a right way:
What you are asking for is: keep on looping until either answer == guess OR i ==5
However, this piece of code actually means:
do{
} while(answer!=guess && i != 5);
"keep on looping if both answer is not guess and i != 5"
which essentially mean the same thing. The reason you find it counter-intuitive is because you are thinking of the "stopping condition" while do-while loop is specifying the "continuation condition"
Some languages (not Java) do provide do-until loop, which is doing what you want. It may look like this if it is provided in Java:
do {
} until (answer == guess || i == 5)
SO i made a CharSequence[] array with all the two consecutive uppercase letters. Why is this code not returning the correct values?
public static boolean containConsecCaps(String passw)
{
CharSequence[] consec = {"AA","AB","AC","AD","AE","AF", ... "ZZ"};
boolean contain = false;
String pass = passw;
for (int i = 0; i <= 675; i++)
{
contain = pass.contains(consec[i]);
if (contain == true)
break;
}
return contain;
}
and in the main string, this syntax:
while (consec != false)
{
//Consec.setPassword(password.getPassword());
consec = Consec.containConsecCaps(password.getPassword());
if (consec == false)
break;
else
password.setPassword(reader.readLine("Error. Please enter a password with no consecutive letters: "));
}
now everything else in the three classes work. This is the only problem and it seems to get stuck in the else. Why is it not testing all of the array char's then returning true or not?
It gets stuck in a loop if i enter: AApassword.1 , which fits all the other criteria, except the two consecutive capitals. So then i enter: aApassword.1 , which should fit ALL of the criteria, but it still prints out the else statement, instead of breaking the while loop.
Can anyone help me?