What would be the best way (or any way) to print out a pattern, that may cut off at any point dependant on the x & y length?

for example, if i wanted a pattern 4 up (y = 4) and 14 across (x = 14)
I would need it to print:

m//m//m//m//m/
m//m//m//m//m/
m//m//m//m//m/
m//m//m//m//m/

I'm thinking a nested for loop so:

for(i=1; i<y; i++){


//then another thing inside so that would make 4 lines total, but with the pattern printing along the x axis…

but how??

Thanks in advance for any help given.

Michael

Dani AI

Generated

The pattern is just the first X characters of the infinite repetition of the unit "m//", repeated for Y lines. Several replies showed nested loops () and there was useful clarification about terminology from ; 's length->output mapping nails the intended sequence. A clean, readable solution is to build the repeating unit once, truncate to the requested width, then print that same line Y times — simple and O(X + Y) work instead of multiple nested loops.

Example (build once, reuse):

char mainSym = 'm';
String sep = "//";
int X = 14; // desired width
int Y = 4;  // number of lines

String unit = String.valueOf(mainSym) + sep;
StringBuilder sb = new StringBuilder(X + unit.length());
while (sb.length() < X) {
    sb.append(unit);
}
String line = sb.substring(0, X);
for (int i = 0; i < Y; i++) {
    System.out.println(line);
}

Notes and alternatives: this avoids recreating the line for every column and keeps complexity linear in X (plus O(Y) prints). If using Java 11+, unit.repeat(...) can replace the while loop. If memory is critical, generate each line char-by-char using pos % unit.length() to pick the correct character. Watch for edge cases: X <= 0 or Y <= 0 (print nothing), and ensure the builder is long enough before calling substring(0, X) to avoid exceptions.

This approach is simpler and more maintainable than an "if inside for inside while inside for" as reported by , and answers 's concern about optimality.

Recommended Answers

All 8 Replies

Hello 'meowmonkey', not sure if that what you want ... Print a rectangle of y lines and x item on every line
Anw here's the code:

int Y = 4;
int X = 12;

for(int y=0; y < Y; y++){
    for(int x=0; x < X; x++){
        System.out.print("#");
    }
    System.out.println();
}

Just in addition, obviously you would change the "#" in amirbwb's code with whatever you wanted to print.

That would print a whole bunch of "#" right?
I need it to be a pattern like 'm//m//m//' that cuts off at any point, so could be the m, the first or the second /

that makes no sense. either it has a fixed length (the int you pass) or it might be cut off.

No worries, solved it.
Required an if loop inside a for inside a while within another for.

I explained what I wanted at the top
One symbol (I used an "m" in the example") separated by 2 "/" symbols each time
so if it was 5 long it would be m//m/ or 8 long m//m//m/ 6 long being m//m//

well ... that's basically the same as:

System.out.print("#");

but you just had to do something like:

// in the class itself
char symbol = "m";
//in the method:
System.out.println(this.symbol + "//");

also: try and use the right terms. it might be confusing if you start mashing terms together in the wrong way, for instance when you speak of an "if loop". an if statement is a conditional statement, not an iteration (loop).

Hi Stultuske
I think the OP's problem is a bit more interesting than that. Your solution only works if the desired length is a multiple of 3. As I understand it the requirement is

 y   output
 -   ------
 1   m
 2   m/
 3   m//
 4   m//m
 .   ...
 9   m//m//m//
10   m//m//m//m

etc

: can you post your solution (if that's not a problem!) so that we can have a look if what you did was indeed the best that could have been done...
Well, you asked for the best way and having 3 levels of iteration (for->while->for) doesn't sound quite optimal...

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.