public static void drawIndentedLine (int spaces, String begin, int n, String middle, String end)

It should print a single line, terminated with a newline, that begins with the specified number of spaces, followed by the begin string, followed by n copies of the middle string, followed by the end string.

I dont know how to do this in eclipse any ideas ?

Dani AI

Generated

This is an IDE-agnostic task (as noted): the solution is pure Java. A reliable approach is to sanitize inputs, build the line with a StringBuilder (pre-size it when possible for performance), then print it with System.out.println so the line ends with a newline. pointed to a related thread for drawing patterns; the implementation below follows the same idea but includes basic defensive checks.

public static void drawIndentedLine(int spaces, String begin, int n, String middle, String end) {
    if (spaces < 0) spaces = 0;
    if (n < 0) n = 0;
    begin = (begin == null) ? "" : begin;
    middle = (middle == null) ? "" : middle;
    end = (end == null) ? "" : end;

    StringBuilder sb = new StringBuilder(spaces + begin.length() + n * middle.length() + end.length());
    for (int i = 0; i < spaces; i++) sb.append(' ');
    sb.append(begin);
    for (int i = 0; i < n; i++) sb.append(middle);
    sb.append(end);

    System.out.println(sb.toString());
}

For Java 11+ a terser option uses String.repeat:

// Java 11+
String line = " ".repeat(spaces) + begin + middle.repeat(n) + end;
System.out.println(line);

Troubleshooting notes: negative counts and null strings are guarded above. For very large n or long middle strings, avoid building one giant string (stream parts to output with System.out.print in a loop and finish with System.out.println), since memory may be exhausted. To test in Eclipse, add the method to any class and invoke it from main; the IDE itself does not affect the core logic.

Recommended Answers

All 2 Replies

the ide you use is irrelevant. this is very basic, using a few for loops, and some print statements.
basic String concatenation and System.out.println statements. just show what you have got so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.