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 }
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
recently my teacher showed us how to make a summ methode but i cant remember it.I think i got it a bit ritght.But it tells me error unexpected token a
void main(){
summ(6, 7);
}
int summ(a, b){
a = int
b = int
return a+b }
It should be like this
static int summ(int a, int b)
{
return a+b;
}
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 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 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
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.