java array to print square box of asterisk - java

I'm trying to write a square shape of asterisk around border using the https://stackoverflow.com/a/34209565 answer.
But I cannot get it working at all.
Here is the code I'm trying.
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
System.out.println();
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i - 1|| j == _j - 1){
array[i][j] = "*";
System.out.print(array[i][j]);
}
}
}
The output I'm getting is:
**********
**
**
**
**
**
**
**
**
**********
I tried running tthe code from the answer but it produces a one line of asterisk. Something from the code in the answer has been omitted.
The code provdied in the answer is:
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i-1|| j == _j-1){
array[i][j] = "*";
}
}
}
And the output in the answer is:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********

You need an else to print a space when the entry is not a star. Also, I would move the println() to the end of the loop (instead of the beginning). Assuming you actually want to fill the array too, populate it with a space as well. Like,
for (int j = 0; j < _j; j++) {
if (i == 0 || j == 0 || i == _i - 1 || j == _j - 1) {
array[i][j] = "*";
} else {
array[i][j] = " ";
}
System.out.print(array[i][j]);
}
System.out.println();
Outputs
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********

You can use the repeat method, so the code is less verbose, also enclosing the generation in a method you can make it dynamic bu setting two arguments: number of lines and number of columns:
class Test
{
public static void main(String[] args)
{
System.out.println(createBox(10, 10));
System.out.println(createBox(10, 5));
}
public static String createBox(int qtaRig, int qtaCol)
{
String result = "";
for(int r = 1; r <= qtaRig; r++)
{
if(r == 1 || r == qtaRig)
{
result += "*".repeat(qtaCol);
}
else
{
result += "*" + " ".repeat(qtaCol-2) + "*";
}
result += "\n";
}
return result;
}
}
Result:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
*****
* *
* *
* *
* *
* *
* *
* *
* *
*****

Related

Printing pattern using Jagged Array

Given the jagged Array, we are asked to use a looping statement to display the character based on the position. Display a "*" if the position matched or a " " if it doesn't.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
I have created a program, but the output is not what I expected and I am now stuck.
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
The output was suppose to be Happy but I get:
enter image description here
It's because of your program does not compare the first j value (which is 0) with itself. Since every value equals itself you can add manually * for each line like this.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
for (int i = 0; i < arr.length; i++)
{
System.out.print("*");
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
Try this.
for (int i = 0; i < arr.length; i++) {
for (int j = 0, p = -1; j < arr[i].length; p = arr[i][j++])
System.out.print(" ".repeat(arr[i][j] - p - 1) + "*");
System.out.println();
}
output:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *

Create a Square of stars with an inputed number

I need a code to help me print a square of stars with an Integer input (in this case the number is 5)
the square must be empty on the inside.
for example:
Looking for this output
* * * * *
* *
* *
* *
* * * * *
what I get
* * * *
*
*
*
* * * *
I am missing my right side of the square.
MY code:
System.out.print("Please enter a number: ");
side = input.nextInt();
for (int i = 0; i < side - 1; i++) {
System.out.print("* ");
}
for (int i = 0; i < side; i++) {
System.out.println("*");
}
for (int i = 0; i < side; i++) {
System.out.print("* ");
}
}
input
5
output
* * * *
*
*
*
* * * *
You can do this with a nested for loop.
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Print a * if it is either first row/column or last row/column; otherwise print two spaces.
It can be done using a single For Loop with a Complexity of O(n)
public static void main(String[] args) {
System.out.print("Please Enter a Number ");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number >= 3) {
String endPattern = new String(new char[number]).replace("\0", " *");
String midPattern = String.format("%-"+(number-2)*2+"s %s", " *"," *");
for(int i=1; i<= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
Output (for input 3)
Please Enter a Number 3
* * *
* *
* * *
output (for input 7)
Please Enter a Number 7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
You need to add an extra star at the end of the middle for loop. This can be done by nested a second for loop of spaces followed by printing the star.
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print(" ");
}
System.out.println("*");
}

Making a method that creates an image with X in square using stars

I am supposed to complete the inside of a method
public static void printXinSquare(int width) {
}
width being the number of rows so that it would create
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
I've tried making four separate pieces of triangles, and attach them somehow, but that didn't work.
I also tried to create a for loop for spaces and then add a for loop for the stars, but I'm confused and have no idea as to how to do that.
To do this, as I said before, I tried to make separate trianges.
public static void printXinSquare(int width) {
for (int i = 1; i <= width/2+1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = width/2+2; i <= width; i++) {
for (int j = width+1-i; j >= 1; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
This creates a triangle pointing to the right.
public static void printXinSquare(int width) {
for (int line = width/2+1; line >1; line--) {
for (int i = 1; i <= (line - 1); i++) {
System.out.print(" ");
}
for (int i = 1; i <= (width - 2 * line); i++) {
System.out.print("*");
}
System.out.println();
}
This creates a triangle pointing upwards.
This is what I have tried so far, but I don't think this way works.
I think I should create for loops that take into account the spaces,
but I don't know how to do that as the spaces are in diagonal direction.
Any help to complete this method would be greatly appreciated :)
This should print in with an input width of 10 the exact picture you have in your question.
public static void printXinSquare(int width)
{
for (int k = 0; k < width; k++) {
for (int j = 0; j < width; j++) {
if (k == j || k == width - j - 1) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
}
It prints * with a blank space every time to ensure it is properly spaced out like that picture, and prints 2 spaces instead in two different cases, each of which account for a single diagonal line. The two cases are as follows:
if the row count is equal to the current column count, there should be a space instead of an asterisk. This will create the first diagonal line going from top left to bottom right.
if the row count is equal to the width minus the current column count minus 1, it also should be a space rather an asterisk. This will create the second diagonal line going the opposite direction.
Try something like this:
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
if (i == j || i+j == width) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println("");
}
In this case you would be printing '*' when it is not in the triangle, and ' ' otherwise. The inside triangle is the condition (i==j || i+j==width)

Printing a * pattern

So I have to print the following pattern by accepting a value of n
Input : 7
Output has to be this :
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
code:
public static void printPattern(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.println("*");
}
System.out.println("\n");
}
for (int a = n-1; a >= 1; a--)
{
for (int b = 1; b <= a; b++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
But for some reason it prints this pattern(say n=8):
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*******
******
*****
****
***
**
*
What is the mistake here?
Use System.out.print instead of System.out.println in the first for loop, the latter one always appends a newline character at the end which is what you are trying to do manually.
And either do System.out.print("\n"); OR System.out.println(""); to add the newline after the inner loop iteration.
System.out.println already adds a line break at the end, therefore System.out.println("\n") adds two line breaks.
The code can be condensed to a single double-for loop. The following routine accepts a single parameter which defines the maximum number of '*' on a single line:
/**
* #param width
* the maximum width of the pattern.
*/
public static void print(int width) {
for (int i = 1; i < 2 * width; ++i) {
for (int j = 0; j < width - Math.abs(width - i); ++j)
System.out.print("*");
System.out.println();
}
}
public static void main(String[] args) {
print(4);
}
Output:
*
**
***
****
***
**
*
first for loop should be like this,
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
System.out.println();
always move the cursor to a new line
in your code when we use System.out.println("\n"); this will move cursor to 2 lines
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int a = n - 1; a >= 1; a--) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
By changing println to print you won't go to another line after each * and remove the \n to not skip 2 lines because println it's already there

relativelyprime number matrix

i want to create a NxN matrix that takes N from the command line matrix such that the
element in row i and column j (1 ≤ i, j ≤ N) is a "*" (a star) if i and j are relatively
prime (ie, GCD(i, j) = 1) and " " (a space) otherwise. The row numbers should be
printed at the end of each row.
public static boolean[][] relativelyPrime(int N) {
boolean[][] array = new boolean [N+1][N+1];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if ( 1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i,j) == 1){
return array;
}
}
}
}
return array;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
boolean[][] matrix = relativelyPrime(N);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length: j++) {
if (1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i, j) == 1) { System.out.println("*" + i);
}
else { System.out.println(" " + i);
}
}
}
}
i am trying to create a matrix that looks like this:
* * * * * * * * * * 1
* * * * * 2
* * * * * * * 3
* * * * * 4
* * * * * * * * 5
* * * 6
* * * * * * * * * 7
* * * * * 8
* * * * * * * 9
* * * * 10
but im getting
*1
*1
*1
*1
*1
*1
*1
*1
*1
*1
*2
2
*2
2
.
.
.
how do i put this in a matrix
You should use System.out.print instead of System.out.println like this:
public static void main(String[] args) {
int N = 11;
boolean[][] matrix = relativelyPrime(N);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i, j) == 1) { System.out.print("*");
}
else { System.out.print(" ");
}
}
}
System.out.println(i);
}
}
In this case, it will give you zero at the first line, to eliminate this use additional if statement:
}
if(i!=0)
System.out.println(i);
}

Categories

Resources