6
\$\begingroup\$

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.

  1. User will be asked for the option of a function to demonstrate.

  2. 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.

  3. Program will receive user inputs and print the resultant array.

  4. 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");
    }
}
\$\endgroup\$
  • \$\begingroup\$ I hope you get great reviews! \$\endgroup\$ – Mathieu Guindon Mar 7 '17 at 20:45
4
\$\begingroup\$

Typos, etc.

        default:System.out.println("Invaild Option, Please choose a vaild option...");
            break;

There are a couple typos in this, and it's odd that elsewhere you put commands on separate lines from cases and on the same line in this.

        default:
            System.out.println("Invalid Option, Please choose a valid option...");

Also, you don't need a break in the last case. The switch will end when it runs out of commands. I'll often put it in anyway if everything is an actual case, but I usually omit it for a default that is at the end.

            System.out.println("Do you wish to test another array? "
                                + "Press any button to contiune or "
                                + "enter 0 to Exit one-dim array test.");

This has another typo on "continue".

            System.out.println("Do you wish to test another array? "
                                + "Press any button to continue or "
                                + "enter 0 to Exit one-dim array test.");

Remember that there are two of these. I'm guessing that this was a copied and pasted error.

The dangers of magic numbers

    for(int i = 0; i < resultArray.length; i++){
        resultArray[i] = getNumber(input, i);

        if(i + 1 < 5){
            System.out.println(", next one");
        }
    }

I'm guessing that at one time the array was of size 5. But now it isn't. The display will be wrong for other sizes.

    resultArray[0] = getNumber(input, 0);
    for (int i = 1; i < resultArray.length; i++) {
        System.out.println(", next one");
        resultArray[i] = getNumber(input, i);
    }

This way avoids the redundant check on each iteration at the cost of writing out the explicit first one.

\$\endgroup\$
1
\$\begingroup\$

Here are my suggestions:

  1. Use PascalCase for class names -> BasicArray instead of basicArray

  2. Your code will be more readable if you use mnemonics variable's names.

  3. The following methods could be refactor into one -> getA1Size, getA2Row & getA2Col, so instead your would have something like:

    private static int getNumber(String message) {
        int result = 0;
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println(message);
            result = scanner.nextInt();
        } catch (InputMismatchException exception) {
            System.out.println("Please input a vaild integer.");
        }
        return result;
    }
    

    so you'll invoke the method like this:

    int row = getNumber("Please enter the row# of this array:");
    
  4. Use camelCase for variable names.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.