Printing on the same line in DrJava using System.Out.Print

1k views Asked by At

I am trying to print a grid using DrJava IDE. However my output is displayed in a row, despite the fact that I used System.out.Print() in my inner loop. Why is this happening?

public class Percolation
{
    private boolean[][] grid;
    private int N = 0;
    private boolean[] conv;

    public Percolation(int N)               // create N-by-N grid, with all sites blocked
    {
        this.N = N;
        checkNegative(N);
        grid = new boolean[N+1][N+1];
    }

    private void checkNegative(int N)
    {
        if( N <= 0)
            throw new java.lang.IllegalArgumentException("Number of sites less than 1");
    }
    private void checkBounds(int i, int j)
    {
        if(( i > N )||( j > N))
        {
            throw new java.lang.IndexOutOfBoundsException("Index out of bounds- " + i + "," + j + " out of bounds");
        }
        else
            return;
    }

    private  boolean[] convertTo1D(boolean g[][])
    {
        int i,j; int k = 0;
        boolean[] conv = new boolean[N*N];
        for(i = 1; i <= N; i++)
            for(j =1; j <= N; j++)
            {
                conv[k] = g[i][j];
            }

        return conv;
    }

    private void displayGrid()
    {
        int i,j;
        for(i = 1; i < N+1; i++)
            for(j =1; j < N+1; j++)
            {
                {

                    System.out.print(i + " " + j);

                }
                System.out.println(" ");
            }

    }

    public void open(int i, int j)          // open site (row i, column j) if it is not open already
    {

        checkBounds(i,j);

        if (isOpen(i,j) == true)
            return;
        else
            grid[i][j] = true;
    }

    public boolean isOpen(int i, int j)     // is site (row i, column j) open?
    {
        checkBounds(i,j);

        return grid[i][j];
    }

    public boolean isFull(int i, int j)     // is site (row i, column j) full?
    {
        checkBounds(i,j);

        return grid[i][j];
    }

    public boolean percolates()             // does the system percolate?
    {
        return false;
    }


    public static void main(String[] args)   // test client (optional)
    {
        int K;
        System.out.println("Enter grid length");
        Scanner in = new Scanner(System.in);
        K = in.nextInt();

        Percolation Perc = new Percolation(K);

        WeightedQuickUnionUF join = new WeightedQuickUnionUF(K);

        Perc.displayGrid();


    }

}

Output -

1 1 
1 2 
1 3 
1 4 
1 5 
1 6 
1 7 
1 8 
1 9 
1 10 
2 1 
2 2 
2 3 
2 4 
2 5 
2 6 
2 7 
2 8 
2 9 
2 10
3 1 
3 2 
3 3 
3 4 
3 5 
3 6 
3 7 
3 8 
3 9 
3 10 
4 1 
4 2 
4 3 
4 4 
4 5 
4 6 
4 7 
4 8 
4 9 
4 10 
5 1 
5 2 
5 3 
5 4 
5 5 
5 6 
5 7 
5 8 
5 9 
5 10 
6 1 
6 2 
6 3 
6 4 
6 5 
6 6 
6 7 
6 8 
6 9 
6 10 
7 1 
7 2 
7 3 
7 4 
7 5 
7 6 
7 7 
7 8 
7 9 
7 10 
8 1 
8 2 
8 3 
8 4 
8 5 
8 6 
8 7 
8 8 
8 9 
8 10 
9 1 
9 2 
9 3 
9 4 
9 5 
9 6 
9 7 
9 8 
9 9 
9 10 
10 1 
10 2 
10 3 
10 4 
10 5 
10 6 
10 7 
10 8 
10 9 
10 10 

Is there something wrong with my code or does it have something to do with the IDE?

1

There are 1 answers

0
Nir Alfasi On BEST ANSWER

You have a couple of extra curly braces that confuses you:

for(j = 1; j < N+1; j++)
{
    {

        System.out.print(i + " " + j);

    }
    System.out.println(" ");
}

what you probably want to do is:

for(j = 1; j < N+1; j++) 
{
    System.out.print(i + " " + j);    
}
System.out.println(" ");