Java

Samuel Zink
Samuel Zink
4,427 Points

Return statement

I don't know how to make return a statement return a value inside the scope of a for statement

public static String betweenPrime(int num1, int num2) { for(int i = num1; i<num2; i++){ numbers += ""; if(isPrime(i)) { numbers += i + ", "; } } return numbers; }

I want to make a list of primes in between two numbers but I don't know how to return the answer, I am making this in the main class and it doesn't have a class, is there another way to do it, thanks.

2 Answers

Bramyn Payne
Bramyn Payne
11,786 Points

First, you need to declare the return type in your method declaration. Which it looks like you want a list of integers instead of a string, so you could do this:

public static List<int> betweenPrime() {}

Then you would need to instantiate a new empty list. Then inside your for loop, you could append the result to your list and return the list afterward.

Also, check Markdown Cheatsheet to see how to get your code more readable.

Hope this helps!

Samuel Zink
Samuel Zink
4,427 Points

P.S. I know there are other errors in my code but the only thing I have issues with is the return statement