Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to count the number of items in a scanner object which are divisible by 2.
My code looks like this:
while (s.hasNext()) {
num = s.nextInt();
if ((num % 2) == 0); {
count++;
}
}
For every integer in the object though, count is increasing by 1, regardless if it is divisible by 2, or not. Can someone tell me what I'm doing wrong?
You have a semicolon (;) after your if clause. That means empty code is executed if the condition is true and the code in the code block is always executed.
The ; after the if should be omitted
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is a segment of the code for my program. What I'm trying to do is match the health of the enemy with the random enemy that get selected. My plan is to make a condition for when the randomInt selects a number so I can print out the correct enemyHealth value from my array. Is there any way I can set the a condition for when rand.nextInt is equal to some number?
if(rand.nextInt(enemy.enemyType) = 0)
System.out.println(enemy.enemyHealth[0]);
if(rand.nextInt(enemy.enemyType) == 0)
System.out.println(enemy.enemyHealth[0]);
= is an assigning operator. It assigns a variable a value and that is not a variable. == is a comparing operator for primitive type variables.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The value of sum is not changing even if I am adding it inside the for loop. Value of diff is 3
The value which is getting printed is like 3 3 3 in initial iteration which should be 3 6 9.
Can someone please help?
loop start
sum = sum + diff<(26-diff)?diff:(26-diff);
loop end
Order of precedence in how your numeric expression is being calculated is what's causing you to get the wrong result. Your current statement is equivalent to this:
sum = (sum + diff)<(26-diff)?diff:(26-diff);
and so equates to the value of diff, which is always 3. Change your statement to this:
sum = sum + (diff<(26-diff)?diff:(26-diff));
and you'll get the behavior you're expecting. you can also use the += operator to fix this by changing your statement to:
sum += diff<(26-diff)?diff:(26-diff);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}
You add to the players again: players.add, instead of weaklings.add.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
If I have an if statement that includes a string like this:
double GUInumber1 = ((GUInumber1 >= 0 || <= 0)? Double.parseDouble(GUIfirstNumber) : 0);
('GUIfirstNumber' is the string)
Why does it come up as a error? Do I need brackets somewhere, do I need to use 'If/else' instead?
Supposedly my compiler says it does not recognize or (||), is this supposed to be something else or does this work in a completely different way.
Any help on this situation would be appreciated, also, if you need to know or are just wondering why I want to make a if statement for a string its because whenever I try to put in a letter instead of a number Java crashes, I'm hoping I could get this to alternatively solve the situation instead.
EDIT:
Solved, had to remove 'or' statements and alternatively make more else statements instead.
This expression (GUInumber1 >= 0 || <= 0) is not correct.
You will need something on both sides of the <= operator.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.