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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
So I wrote this function and for the life of me cannot work out what is wrong with it. It is an extremely basic function yet when I try and compile it I get
LoopArraysLibrary.java:4: error: cannot find symbol
for (var i=0, x=0, y=1, x1=0;i<5;i++)
^
symbol: class var
location: class LoopArraysLibrary
1 error
This is the function:
public static int fib(int n) {
for (var i=0, x=0, y=1, x1=0;i<5;i++)
{
return(x);
x1=x;
x=y;
y=x1+y;
}
}
There is no keyword or built-in type name var in Java. (Java is NOT Javascript!!)
It looks like those variables need to be int, so you could declare the variables like this:
for (int i=0, x=0, y=1, x1=0;i<5;i++)
{ ...
However, that's not enough because the first statement in the loop body is
return(x);
and that is going to cause the Java compiler to say that the statements after are unreachable code. Based on what you are trying to implement, your code should be returning after the loop. And that means that x, y and x1 should not be declared as local to the loop.
I'll leave you to work out how to implement that ... and find / fix the other problem :-)
UPDATE: Starting with Java 10 (release March 2018) the language supports var for local variables.
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 last month.
Improve this question
I watched a video about composition and inheritance.
In this video I saw two different examples of code syntax that I have not seen before.
Generally the code looks like java but I don't recognize these two code snippets.
Would appriciate if someone would explain what the code does and if the code is java or another language.
Thanks!
First code:
public void replacePixel(Pixel[,] pixels] {....}
Here it is the syntax [,] that is new to me.
What does it do?
Second code:
void saveClicked(){
file?.load(image);
}
Here is is the syntax ?.
What does it do?
Tried to use a online java compiler and the syntax did not seem to work.
public void replacePixel(Pixel[,] pixels) {....}
Im not sure if this is valid for Java, but in C#:
[,] here means that as an argument you want to get two-dimensional array. If you want three-dimensional array you can use [,,] and so on.
void saveClicked(){
file?.load(image);
}
Here ? is a conditional operator that says: if file is not null then do file.load(image). Otherwise, if file is null do nothing. This can be converted to:
void saveClicked(){
if(file != null){
file.load(image);
}
}
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I'm having an issue with multiple NullPointerExceptions, I'm getting one for my if statements within iterators and I can't figure out why
one of the problem blocks:
public int removeAllBooks(String author, String title){
Iterator<Book> itr=library.iterator();
int i=0;
while(itr.hasNext()){
Book book=itr.next();
if(b.getAuthor().equals(author)&&(b.getTitle().equals(title))){
itr.remove();
i++;
}
if(i>0){
return i;
}
}
return 0;
}
The NPE error points to my if-statement line for some reason.
Thanks.
What is b? Where is it declared/initialized? Your itr.next() is assigned to the book variable, not b.
You code should probably be :
Book book=itr.next();
if(book.getAuthor().equals(author)&&(book.getTitle().equals(title))){
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 }
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.