Testing quicksort user input I am testing a quicksort implementation. Arrays of random size with random data are passed to quicksort and the return time is averaged out. The user is prompted to enter values for


minimum length of array
maximum length of array
maximum value in array
minimum value in array
if the user wishes to see the sorted and unsorted arrays
if the user wishes the program to check itself by testing to make sure the array was sorted properly 


I tried breaking up the prompts for user input into several methods but things are getting ugly. Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").

public static void main(String[] args)
{
    getInput();
}

public static void getInput()
{
    int bound = getNumber("How many arrays should be generated for quicksort?");
    int minVal = getNumber("Minimum value of element:");
    int maxVal = getNumber("Maximum value of element:");
    int minLength = getNumber("Minimum length of an array:");
    int maxLength = getNumber("Maximum length of an array:");
    boolean giveOutput = yes_no("Would you like to see the sorted and unsorted arrays?");
    boolean checkCorrectness = yes_no("Check proccessed arrays to see if sorted correctly?");

    long totalTime = 0, sumArraySizes = 0;

    for(int i = 0; i &lt; bound; i++)
    {
        int[] sample = generateRandomArray(minLength, maxLength, minVal, maxVal);
        sumArraySizes += sample.length;
        if(giveOutput)
            System.out.println(Arrays.toString(sample));
        long startTime = System.nanoTime();//was using System.currentTimeMillis(); but endTime and startTime would always be the same
        qs(sample, 0, sample.length-1);
        long endTime = System.nanoTime();
        totalTime += endTime-startTime;
        if(giveOutput)
            System.out.println(Arrays.toString(sample)+"\n");
        if(checkCorrectness)
            if(!isSorted(sample))
                System.out.println("WARNING! mistake detected");
    }

    System.out.println("Average size of array: "+sumArraySizes/bound);
    System.out.println("Average time taken to sort array: "+totalTime/bound+" nanoseconds");
}

/*prompts user with a message and returns number they enter*/
public static int getNumber(String message)
{
    Scanner sc = new Scanner(System.in);
    System.out.println(message);
    return sc.nextInt();
}

/*http://stackoverflow.com/questions/15927885/yes-no-with-boolean-or-if-else*/
public static boolean yes_no(String message)
{
    Scanner sc = new Scanner(System.in);
    String answer;
    boolean yn;

    System.out.print(message);
    OUTER:
    while (true) {
        answer = sc.nextLine().trim().toLowerCase();
        switch (answer) {
            case "y":
                yn = true;
                break OUTER;
            case "n":
                yn = false;
                break OUTER;
            default:
                System.out.println("Please answer with y or n");
                break;
        }
    }
    return yn;
}


What should the method yes_no be named? I wanted to call it yesOrNo but that looked strange.
Spaces

Put a space after for and if:


for(int i = 0; i &lt; bound; i++) --> for (int i = 0; i &lt; bound; i++)
if(giveOutput) --> if (giveOutput)


Put a space around a -:

sample.length-1 --> sample.length - 1

And the same goes for +:

System.out.println("Average time taken to sort array: "+totalTime/bound+" nanoseconds");


becomes:

System.out.println("Average time taken to sort array: " + totalTime/bound + " nanoseconds");


if-statements

Add braces around if-statements:

if (giveOutput) {
    System.out.println(Arrays.toString(sample));
}


Avoid nested if-statements such as:

if(checkCorrectness)
    if(!isSorted(sample))
        System.out.println("WARNING! mistake detected");


Change that to:

if (checkCorrectness &amp;&amp; !isSorted(sample)) {
    System.out.println("WARNING! mistake detected");
}


(Consider throwing an exception for this by the way. A mistake is important stuff)

Naming

Whatever qs is doing (A quicksort I guess), the name is too short to be meaningful. Name it quicksort instead.

Name your methods for inputting things as input*:


getNumber --> inputNumber
yes_no --> inputBoolean



  Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").


I believe this can be answered better on User Experience. They probably have a question about it already.

I would think that a question mark should be used when asking a question, and a colon should be used when encouraging the user to enter something. Of course this doesn't say much though as all your user prompts can be modified to the other form. My opinion is that what you currently have is good though.

Other comments


Good choice to use System.nanoTime();
To make the existence of another line in your output clear, I would change System.out.println(Arrays.toString(sample)+"\n"); to:

if (giveOutput) {
    System.out.println(Arrays.toString(sample));
    System.out.println();
}

In Java, the convention is that { goes on the same line, not on it's own line.

public static int getNumber(String message)
{


should be, according to the conventions:

public static int getNumber(String message) {

Avoid multiple variable declarations on the same line

long totalTime = 0, sumArraySizes = 0;


can be:

long totalTime = 0;
long sumArraySizes = 0;