I wrote this demo just for practice purpose, I would like to get some helps that could further improve my code quality.
This code is a simple demonstration of 1-D and 2-D array.
User will be asked for the option of a function to demonstrate.
Once user made the choice (either 1-D or 2-D), the corresponding function will ask user for size of the array and elements that user wants to put into the array.
Program will receive user inputs and print the resultant array.
After the print is completed, program will continue asking for new array, until user want to exit the function.
import java.util.InputMismatchException;
import java.util.Scanner;
public class basicArray {
public static void main(String[] args) {
System.out.println("Welcome to basic array test, please choose your testing subject: ");
//try-with-resources method to close the main scanner
try(Scanner user_option = new Scanner(System.in)){
mainMenu(user_option);
}//close the I/O before the program end
System.out.println("Program terminated...");
}
private static void mainMenu(Scanner input){
int option = 0;
do{
System.out.println("Option 1: Simple one dimensional array.");
System.out.println("Option 2: Simple two dimensional array.");
System.out.println("Option 0: Exit program");
option = input.nextInt();
System.out.println("Current option is: " + option);
switch(option){
case 1:
constructOneDimArray();
break;
case 2:
constructTwoDimArray();
break;
case 0:
return;
default:System.out.println("Invaild Option, Please choose a vaild option...");
break;
}
System.out.println("-Main Menu-");
}while(option != 0);
}
//--------------1-dim array methods-----------------
private static void constructOneDimArray() {
int option_a1 = 1;
Scanner sc_SimpleA1 = new Scanner(System.in);
do{
int size = getA1Size();
int a1[] = buildOneDimArray(size);
//print function for 1-dim array
printA1(a1);
System.out.println("Do you wish to test another array? "
+ "Press any button to contiune or "
+ "enter 0 to Exit one-dim array test.");
option_a1 = sc_SimpleA1.nextInt();
}while(option_a1 != 0);
System.out.println("\nEnd of testing 1, back to main menu...");
}
private static int getA1Size(){
int sizeA1 = 0;
Scanner sc_getA1Size = new Scanner(System.in);
try{
System.out.println("Please enter the size of this array:");
sizeA1 = sc_getA1Size.nextInt();
}catch(InputMismatchException exception){
System.out.println("Please input a vaild integer.");
}
return sizeA1;
}
private static int[] buildOneDimArray(int size){
int[] resultArray = new int[size];
Scanner input = new Scanner(System.in);
for(int i = 0; i < resultArray.length; i++){
resultArray[i] = getNumber(input, i);
if(i + 1 < 5){
System.out.println(", next one");
}
}
return resultArray;
}
private static int getNumber(Scanner input, int index){
System.out.println("Request input for item " + index);
int result = input.nextInt();
System.out.print("Got it");
return result;
}
private static void printA1(int[] a1){
System.out.println("\nThe numbers in array_1 are: ");
for(int val: a1){
System.out.print(val + " ");
}
System.out.println("\n");
}
//--------------2-dim array methods-------------------
private static void constructTwoDimArray() {
int option_a2 = 1;
Scanner sc_SimpleA2 = new Scanner(System.in);
do{
int row = getA2Row();
int col = getA2Col();
int a2[][] = buildOTwoDimArray(row, col);
//print function for 2-dim array
printA2(a2);
System.out.println("Do you wish to test another array? "
+ "Press any button to contiune or "
+ "enter 0 to Exit two-dim array test.");
option_a2 = sc_SimpleA2.nextInt();
}while(option_a2 != 0);
System.out.println("\nEnd of testing 2, back to main menu...");
}
private static int getA2Row(){
int a2Row = 0;
Scanner sc_getA2Row = new Scanner(System.in);
try{
System.out.println("Please enter the row# of this array:");
a2Row = sc_getA2Row.nextInt();
}catch(InputMismatchException exception){
System.out.println("Please input a vaild integer.");
}
return a2Row;
}
private static int getA2Col(){
int a2Col = 0;
Scanner sc_getA2Col = new Scanner(System.in);
try{
System.out.println("Please enter the col# of this array:");
a2Col = sc_getA2Col.nextInt();
}catch(InputMismatchException exception){
System.out.println("Please input a vaild integer.");
}
return a2Col;
}
private static int[][] buildOTwoDimArray(int row, int col){
int [][]resultArray = new int[row][col];
Scanner input = new Scanner(System.in);
for(int i = 0; i < resultArray.length; i++){
for(int j = 0; j < resultArray[0].length; j++){
resultArray[i][j] = getNumberA2(input, i, j);
}
/*
if(i + 1 < 5){
System.out.println(", next one");
}
*/
}
return resultArray;
}
private static int getNumberA2(Scanner input, int rowIndex, int colIndex){
System.out.println("Request input for item " + rowIndex + ", " + colIndex);
int result = input.nextInt();
System.out.println("Got it");
return result;
}
private static void printA2(int[][] a2){
System.out.println("\nThe numbers in array_2 are: ");
for(int []rolA2: a2){
System.out.print("\n");
for(int i: rolA2){
System.out.print(i + " ");
}
}
System.out.println("\n");
}
}