Haseeb Waseem
Haseeb Waseem

Reputation: 71

Deleting last new line string

So I've tried pretty much everything to get rid of the last newline character in my code. Its supposed to print a new line after every recursive call except for the last one. Any ideas?

public static boolean solve(double target, ArrayList<Double> numbers)
{
    String print = "";
    String newPrint = "";
    double compare = 0;

    boolean done = false;

    for (double num : numbers)
    {
        if (!done)
        {

            ArrayList<Double> remaining = new ArrayList<Double>(numbers);

            remaining.remove(num);

            if (target == num)
            {
                done = true;

            }
            else
            {

                done = solve(target + num, remaining);
                if (done)
                {
                    print += ((int) target + (int) num) + " " + "-" + " " + (int) num + " "
                            + "="
                            + " "
                            + ((int) target + "\n");
                }
                else
                {

                    done = solve(target - num, remaining);
                    if (done)
                    {
                        print += ((int) target - (int) num) + " " + "+" + " " + (int) num + " "
                                + "=" + " "
                                + ((int) target + "\n");
                    }
                    else
                    {

                        done = solve(target * num, remaining);
                        if (done)
                        {
                            print += ((int) target * (int) num) + " " + "/" + " " + (int) num
                                    + " " + "=" + " "
                                    + ((int) target + "\n");
                        }
                        else
                        {

                            done = solve(target / num, remaining);
                            if (done)
                            {
                                print += ((int) target / (int) num) + " " + "*" + " "
                                        + (int) num
                                        + " " + "="
                                        + " " + ((int) target + "\n");
                            }
                        }
                    }
                }

            }
        }

    }

    System.out.print(print);

    return done;
  }
}

Upvotes: 3

Views: 246

Answers (4)

mike
mike

Reputation: 5055

Short Print the new line character before.

The first thing the recursive function should do is print the new line character. This way you shift the odd case from the last function call, to the first call, which should not prepend a new line character.

You could add a parameter String prepend = "" to the signature of the recursive method, it's empty on the first call, all further calls have it set to String prepend = "\n". Then you can just print prepend without bothering with its content.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

For instance:

void recursiveF(...) {
    if ... {
        recursiveF(...);
        println();
    }
    ...
}

Upvotes: 1

Selvapriya
Selvapriya

Reputation: 1

Use trim() method. It will remove white spaces, newline etc from beginning and end of string.

System.out.print(print.trim());

Upvotes: 0

deamentiaemundi
deamentiaemundi

Reputation: 5525

The result is a string but you don't want the last character, so remove it:

print = print.substring(0, print.length()-1);

Upvotes: 0

Related Questions