Write a program to find the greatest number in a 3*3 array.
The program is supposed to receive 9 integer numbers as command-line arguments.
Program to Print 3*3 array in Java
Reading 9 integer numbers as command-line arguments and assigning it to 3*3 matrix and printing it
Program Steps are
- Create a Java class
- declare a 3 by 3 matrix
- create two loops to read two dimensional array outer loop is for row and inner loops is for column
- Access command line argument values and store to array.
- display the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class GreatestNumber { public static void main(String args[]) { int a[][]=new int[3][3]; int i=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ a[row][col]=Integer.parseInt(args[i]); i++; } } System.out.print("The given array is :"); for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ System.out.print(" "+a[row][col]); } } } } |
Here a[row][col]=Integer.parseInt(args[i]);
row and column is incremented by loop and to increase command line argument value variable i
is used.
Output of above program is as below

Above program works properly if you provide correct number of arguments.
if argument is less then the desired count then it throws ArrayIndexOutOfBoundsException

Checking Number of command line arguments in Java
As we already know String args[] is a command line argument, that is a String array.
To Check the array length we use length property of array in java.
Following code must be included to check the args length
1 2 3 4 | if(args.length!=9){ System.out.println("Please enter 9 integer numbers"); System.exit(0); } |
Complete code after including this is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class GreatestNumber { public static void main(String args[]) { if(args.length!=9){ System.out.println("Please enter 9 integer numbers"); System.exit(0); } int a[][]=new int[3][3]; int i=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ a[row][col]=Integer.parseInt(args[i]); i++; } } System.out.print("The given array is :"); for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ System.out.print(" "+a[row][col]); } } } } |
Expected Input and output as below
example1: c:\>java sample 1 2 3 o/p expected : please enter 9 integer numbers

Program to find greatest number in a 3*3 array
example2: c:\>java sample 1 23 45 55 121 222 56 77 89
o/p expected : the given array is : 1 23 45 55 121 222 56 77 89 the biggest number in the given array is 222
Steps to find Greatest number in array
- Declare an array
- Initialize array
- take a variable (ie greatest) to store greatest number and initialize to 0
- compare each element of array with greatest
- if array element is greater that greater then assign greater=array element
- print array ement
- print greater value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class GreatestNumber { public static void main(String args[]) { if(args.length!=9){ System.out.println("Please enter 9 integer numbers"); System.exit(0); } int a[][]=new int[3][3]; int i=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ a[row][col]=Integer.parseInt(args[i]); i++; } } System.out.print("The given array is :"); int greatest=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ if(a[row][col]>greatest){ greatest=a[row][col]; } System.out.print(" "+a[row][col]); } } System.out.println(" The biggest number in the given array is "+greatest); } } |
To find the greatest among array elements we declared a variable greatest and initialized to 0.
Now each time comparing greatest with array element and if array element is greater than variable greatest the assign array value to greatest.

Java method to find greatest number in a 3*3 array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class GreatestNumber { public static void main(String args[]) { if(args.length!=9){ System.out.println("Please enter 9 integer numbers"); System.exit(0); } int a[][]=new int[3][3]; int i=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ a[row][col]=Integer.parseInt(args[i]); i++; } } GreatestNumber gn=new GreatestNumber(); gn.greatestInArray(a); } public void greatestInArray(int b[][]){ System.out.print("The given array is :"); int greatest=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ if(b[row][col]>greatest){ greatest=b[row][col]; } System.out.print(" "+b[row][col]); } } System.out.println(" The biggest number in the given array is "+greatest); } } |
Java method to return greatest number in a 3*3 array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class GreatestNumber { public static void main(String args[]) { if(args.length!=9){ System.out.println("Please enter 9 integer numbers"); System.exit(0); } int a[][]=new int[3][3]; int i=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ a[row][col]=Integer.parseInt(args[i]); i++; } } GreatestNumber gn=new GreatestNumber(); int largest=gn.greatestInArray(a); System.out.println(" The biggest number in the given array is "+largest); } public int greatestInArray(int b[][]){ System.out.print("The given array is :"); int greatest=0; for(int row=0;row<3;row++){ for(int col=0;col<3;col++){ if(b[row][col]>greatest){ greatest=b[row][col]; } System.out.print(" "+b[row][col]); } } return greatest; } } |