Removing whitespace at the end

Viewed 74

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

Not sure what language you are using. Looks like Java (c# ?). String classes usually have a trim() or strip() method which will remove leading and trailing whitespace.

If you are using c#, consider using string.join() and Enumerable.Range()

To remove the trailing whitespace you'll need to invoke the method on the final value of num. If you are having issues it might be because you have been trimming as you go and so loosing the spaces in between. This is what you should change your return statement to be.

return num.stripTrailing();

There are a few methods in the String class that handle the standard trim type functionality. It's worth getting familiar with the Java docs, here's String in Java 11

Alternatively you may want to gather all the even numbers in an array then use String.join() to build the final string that gets returned.

More elegant than stripping off the final space is to not add it in the first place. Just check if you're at the last iteration of the loop and if so don't add the delimiter.

for (int i = 0; i <= a; i += 2) 
{
    if(i < a - 1)
        num += i + " ";
    else
        num += i;
}

Another common trick is to only add a delimiter on the first n-1 iterations, then just print the final value with no delimiter, doing away with the need for a check in the loop.

int i;
for (i = 0; i < a - 1; i += 2) 
{
    num += i + " ";
}
num += i;

Notice how we have to declare the loop variable i outside of the for statement to make it visible.

Finally, you might consider using a StringBuffer.

You don't have to append a space after the even number, you can just put a space before them, so you don't have to worry about trailing spaces.

  1. First is to set your default answer as 0, so that if the for-loopdoesn't execute it will return 0.
  2. If value of method argument is less than 0 just return NONE
  3. If both are not executed, that means you have a valid range, then its a basic for-loop logic

    public static String even(int amount) {
    
        String output = "0";
    
        if(amount < 0) {
            return "NONE";
        }
        else if(amount == 0) {
            return output;
        }
    
        for(int i=2; i<=amount; i += 2) {
            output += " " + i;
        }
    
        return output;
    }
    
    public static void main(String[] args) {
    
        System.out.println(even(5));
        System.out.println(even(8));
        System.out.println(even(-5));
        System.out.println(even(0));
    }
    

You can use String.join to get the required sequence. You can use below Code

    private static void findEvenSequences(int max) {
    String exp = new String();

    if (max < 0) {
        exp = "NONE";
    } else {
        for (int i = 0; i < max; i = i + 2) {

            exp = String.join(" ", exp, String.valueOf(i));
        }

    }
    System.out.println(exp.trim());

}
Related