Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

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
19,589 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!

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