I have to write a Java method that will return the following as a string:
Example: evens(5); String returned by method: “0 2 4”
Example: evens(8); String returned by method: “0 2 4 6 8”
Example: evens(-5); String returned by method: “NONE”
Example: evens(0); String returned by method: “0”
The problem I am encountering is a whitespace issue at the end of my code. If my code prints evens(5) the string returned by my method will be "0 2 4 ", but I need it to look like the examples above. My code looks like this:
public class Evens {
public static String evens(int a) {
if (a < 0) {
return "NONE";
}
String num = "";
for (int i = 0; i <= a; i += 2) {
num += i + " ";
}
return num;
}
}