How to make a summ Method in Java [closed] - java

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;
}

Related

IndexOutOfBounds error, hangman game [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
I'm making a quick hangman game, and came across an IndexOutOfBoundsException and was wondering why. I don't see the problem/how this error would come about.
It happens at this line:
array[index]+=c;
Any feedback is appreciated.
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[]args) throws IOException {
Scanner console = new Scanner (System.in);
String[] phrase={"television"};
String[] array= new String[phrase.length];
int body =6;
while(array!=phrase) {
char c=(char)System.in.read();
int index= console.nextInt();
array[index]+=c;
if(array[index].charAt(index)==phrase[index].charAt(index)){
System.out.println("the new array");
}
}
}
}
There are many Issues with the code. few of them are below.
you are creating array of size "phrase.length" which will be of size 1, when i enter 2 for "console.nextInt();" it will throw index out of bound.
Array Equality check is wrong, you need to do something like
if( Arrays.equals(array1, array2) )

JAVA error finding prime no. between 1 to 20 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
I'm new and would like to find and correct my errors. I got 2 errors while writing a java program to print prime nos. between 1 to 20.
code:
error:
; instead of ,
for (i=1;i<20;i++)

Iterator NPE in java [closed]

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))){

missing return statement even method is having void return type [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 8 years ago.
Improve this question
am writing a java program as below.
Void fun(int n){
for(int i=0;i<=n;i++){
fun(n-i);
}
System.out.println(“well done”);
}
I am getting error, missing return statement. I not used int or string method. It is void method na. why it asking return type, please help for this problem.
Void is a reference type.
void is a language primitive.
You don't need a return statement when your return type is void.

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 }

Categories