Search code examples
javaarraysfor-loophorizontal-linevertical-partitioning

How to produce horizontal and vertical lines with a for loop?


So this is my class. I'm trying to get the print() method to print out a horizontal line above my array and another line below my array. I also want to have vertical lines between each number in my array. Okay, I fixed my horizontal lines. Now I'm just lost on how to put vertical lines at the beginning and the end of my arrays.

public class Array{

private int[] array;

public Array(int numElements)
{
    array = new int[numElements];
}

public void fill()
{
   for (int i = 0; i < array.length; i++)
    {
        array[i] = (int) (10*Math.random());
    } 
}

public void print()
{
    for(int x = 0; x < 2*array.length; x++)
    {
        System.out.print("-");
    }
    System.out.println();

    for(int i = 0; i < array.length; i++)
    {
        System.out.print(array[i]);
        if(i == array.length - 1)
        {
            System.out.println();
        }
        else
        {
            System.out.print("|");
        }
    }

    for(int n = 0; n < 2*array.length; n++)
    {
        System.out.print("-");

    }
    System.out.println();
}

public void sort()
{
    int n = array.length;
    boolean swapped;

    do
    {
        swapped = false;

        for(int i = 1; i < n; i++)
        {
            if(array[i-1] > array[i])
            {
                int tmp = array[i];
                array[i] = array[i-1];
                array[i-1] = tmp;
                swapped = true;
            }
        }
    } while(swapped);
}

public void printFrequency()
{
    int[] countArray = new int[10];
    for(int x : array)
    {
        countArray[x]++;
    }
    for(int i = 0; i < 10; i++)
    {
        System.out.println("There are " + countArray[i] + ", " + i + "'s ");
    }
}
}

This is my output:

Original:
-----------
1|5|9|3|3
-----------
Sorted:
-----------
1|3|3|5|9
-----------
Frequencies:
There are 0, 0's 
There are 1, 1's 
There are 0, 2's 
There are 2, 3's 
There are 0, 4's 
There are 1, 5's 
There are 0, 6's 
There are 0, 7's 
There are 0, 8's 
There are 1, 9's 

Original:
---------------------
7|0|5|2|0|1|8|0|7|7
---------------------
Sorted:
---------------------
0|0|0|1|2|5|7|7|7|8
---------------------
Frequencies:
There are 3, 0's 
There are 1, 1's 
There are 1, 2's 
There are 0, 3's 
There are 0, 4's 
There are 1, 5's 
There are 0, 6's 
There are 3, 7's 
There are 1, 8's 
There are 0, 9's 

My output should look like this:

Original:
-----------
|1|5|9|3|3|
-----------
Sorted:
-----------
|1|3|3|5|9|
-----------
Frequencies:
There are 0, 0's
There are 1, 1's
There are 0, 2's
There are 2, 3's
There are 0, 4's
There are 1, 5's
There are 0, 6's
There are 0, 7's
There are 0, 8's
There are 1, 9's

Original:
---------------------
|7|0|5|2|0|1|8|0|7|7|
---------------------
Sorted:
---------------------
|0|0|0|1|2|5|7|7|7|8|
---------------------
Frequencies:
There are 3, 0's
There are 1, 1's
There are 1, 2's
There are 0, 3's
There are 0, 4's
There are 1, 5's
There are 0, 6's
There are 3, 7's
There are 1, 8's
There are 0, 9's

Solution

  • Move the line System.out.println(); outside of your for loop if you don't want to create a new line after each dash character.