Strings in ternary operators interacting with numbers [closed] - java

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.

Related

Can you make if/else statements for when rand.nextInt() = 0? [closed]

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.

This code does not compile due to an unreachable statement [closed]

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 6 years ago.
Improve this question
I've tried my utmost to reproduce this in another way, but it seems that only this line gives the error (please excuse the comment):
return foo.containsKey(MARKET_DATA) && !foo.get(MARKET_DATA).isMissing();/*mapping code can inject a Missing type*/;
However I type this it gives me an error: "unreachable statement". Why is this?
This is clear if you remove the comment:
return foo.containsKey(MARKET_DATA) && !foo.get(MARKET_DATA).isMissing();;
Note well the two semicolons at the end: empty statements are allowed in Java, but this particular empty statement is unreachable as the previous statement always returns.
(For the avoidance of doubt, a comment should not be terminated with a ;).
The issue is because of the ; after the comment:
/*mapping code can inject a Missing type*/;
The compiler thinks there is another statement after the return statement. If you delete the ; after the comment it will work fine (or put it inside the comment).
As per Java specs ; is a empty statement. So there is a statement after return

What is wrong with my if-else syntax? [closed]

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 7 years ago.
Improve this question
Currently new to java, and right now I'm doing an assignment involving if else statements. Can someone please why javac is reading my else as dangling? My syntax should be right unless I am missing something here.
You have to remove your Semicolon after the if-statement. Otherwise the braces after the if are not executed.
I hope this will fix your Problem.
if(Statement);
{
//The Content would not be executed in realtion to the if-statemen
}
Otherwise:
if(Statement)
{
//The Content would be executed in realtion to the if-statemen
}
remove the semicolon(;) after the if statement
You have a semicolon after your if. Remove it.
You have to remove the ; at the end of the line starting with if.
change :
if(...);
{
////
}
to
if(...)
{
////
}

Can't create two int in for statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Can you please help me make the following code work?
for(int a=0, b=0; a<101; b<102; a++; b++;) {
stuff
}
You got the initialization (first) part of the loop right.
The termination or condition (second) part of the loop should be evaluated to a boolean, so assuming you require an AND relation between the conditions on a and b, it becomes a<101 && b<102. You might want || (OR) instead, depending on your logic.
The increment (third) part of the loop should contain comma separated expressions (same as the initialization part which you already got right).
I also removed an extra ';' from the end.
for(int a=0, b=0; a<101 && b<102; a++, b++) { stuff }

Mod not working the way I expect [closed]

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

Categories