Java Tutorials

Magic Word Square With Arrays

“Magic Word Square With Arrays” is a simple program for better understanding of arrays in Java.


import java.util.*;
public class magicSqure
{
public static void main (String [] args)
{
char [] [] square = { {'P', 'R', 'E' ,'Y'}, {'L', 'A', 'V', 'A'}, {'O', 'V', 'E', 'R'}, {'T', 'E', 'N', 'D'}};
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter the row number");
int row = sc.nextInt();
displayRow(square,row);
System.out.println("Please Enter the column number");
int column = sc.nextInt();
displayColumn(square,column);
}
static void displayRow (char [] [] squareIn, int rowIn)
{
for (int i = 0; i < squareIn[rowIn].length; i++)
{
System.out.print(squareIn[rowIn][i]);
}
System.out.println();
}
static void displayColumn (char [] [] squareIn, int columnIn)
{
for (int i = 0; i < squareIn[columnIn].length; i++)
{
System.out.print(squareIn[i][columnIn]);
}
System.out.println();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *