-1

I'm trying to get an output of a random string consisted of 1's and 0's in a Matrix style. I know how to display a string consisted of 1's and 0's, but I can't keep on looping through it for some reason. What I'm trying to do, is that whenever the StringBuilder reaches the length of 20, I want to start the loop on a new line again and repeat this 100 times.

import java.util.List;
import java.util.Random;

public class main {

    static Random rand = new Random();
    static StringBuilder x = new StringBuilder();
    static int a = 0;

    public static void main(String[] args) {
        generatingnumber();
    }

    public static void generatingnumber() {

        for (int bv = 0; bv <= 100; bv++) {

            int random = rand.nextInt(50);

            if (random % 2 == 0) {
                x.append(" 0");
            } else {
                x.append(" 1");
            }

            if (x.length() == 20) {
                System.out.println(x);
            }
        }
    }
}
bluevoxel
  • 4,978
  • 11
  • 45
  • 63
user3323950
  • 207
  • 1
  • 5
  • 14
  • 1
    You are forgetting to [reset your `StringBuilder` instance](http://stackoverflow.com/questions/5192512/how-can-i-clear-or-empty-a-stringbuilder) after `println`. – Maarten Bodewes Aug 09 '14 at 23:36

2 Answers2

0
public class MatrixFilm {

    public static void main(String[] args) {
        int rows = 100;
        int cols = 20;

        for (int count1 = 0; count1 < (rows * cols); count1++) {
            for (int count2 = 0; count2 < cols; count2++) {
                int randomNum = 0 + (int) (Math.random() * 2);
                System.out.print(" " + randomNum);
            }
            System.out.println();
        }
    }
}


Result:

 0 0 1 0 0 0 0 1 0 0 1 1 0 0 1 0 1 1 1 1
 1 1 0 1 1 1 0 1 0 1 0 1 1 0 1 1 0 0 1 0
 0 1 0 0 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0
 0 1 0 1 1 1 1 1 0 0 1 1 0 0 0 1 1 0 1 0
 0 0 0 1 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0
 ....
Vy Do
  • 46,709
  • 59
  • 215
  • 313
0

Your string has length of 20 characters only once. You are not interested in whether x.length() == 20 but if x.length() % 20 == 0.

For a new line you can append "\n" (or "\r\n" for Windows machine) to the string, everytime just before printing it.

Change println to print (which doesn't add new line character at the end of printed string) in order to maintain continuity between prints.

Taking all into account:

if (x.length() % 20 == 0) {
  x.append("\r\n");
  System.out.print(x);
}

However it still wouldn't be enough, for "\r\n" itself adds to the length of the string. This should work:

if (x.length() % 20 == 0) {
  x.replace(x.length() - 2, x.length(), "\r\n");
  System.out.print(x);
}

You can also - and it would be better to... - reset the string, as @owlstead has mentioned.

if (x.length() == 20) {
  System.out.println(x);
  x.setLength(0);
}

Anyway; what I presented is not a solution for the problem. Only solution to - probably improper - approach you have currently taken on it.

Sagi
  • 578
  • 1
  • 4
  • 13