zhenyulincs / CSC210-helper-java-class

This helper class provide the Scanner input with validation and get array index.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CSC210-helper-java-class

This helper class provide the Scanner input with validation and get array index. There is some example in the text.java file.

    // Helper myHelper = new Helper();
    // Ask user for input int
    // If you want the user enter the number only greater than 0
    // System.out.print("Ask user for input int: ");
    // int inputInt = myHelper.inputInt("errorMessage", 0);
    // System.out.println(inputInt);
    /**
     * Output: Ask user for input int: a 
     * errorMessage 
     * -1 
     * Plz enter the number
     * greater than 0 
     * 1 
     * 1
     */


     //If you want the user enter the number between 1 and 10
    // System.out.print("Ask user for input int: ");
    // int inputInt = myHelper.inputInt("errorMessage", 1,10);
    // System.out.println(inputInt);
    /* 
    Output: Ask user for input int: a
    errorMessage
    -1
    Plz enter the number between 1 and 10                                                                                                                                                 a
    11                                                                                                                                                                                    \
    Plz enter the number between 1 and 10
    10
    10 
    */


    //Same usage for ask user double.

    //If you want to get the index of an array
    // String[] myArray = {"a","b","c","d"};
    // int index = myHelper.getArrayIndex(myArray, "b", 0);
    // System.out.println(index);
    //Output:1
    // int notExistIndex = myHelper.getArrayIndex(myArray, "3", 0);
    // System.out.println(notExistIndex);
    //Output:-1

About

This helper class provide the Scanner input with validation and get array index.


Languages

Language:Java 100.0%