Member Avatar for gferrie

I have two interfaces, MyapiA and MyapiB with 2 methods each. Two classes - SortA does all the sorting routines and SortB defines the arrays and generates random numbers.

In the main class, Sort I have the following code which produces the expected results using a default argument of 6 for the "x length" or number of numbers to sort. This all works well, where I am a bit stuck is getting it to accept user input or use the default value. I could hand the assignment in without this but I really want to learn how this can be done. I have played a bit with the Scanner object but am not really sure how to proceed from this point. Here is the code from my Sort class which is very simple:

public class Sort extends SortB {

// start the main method
static public void main(String[] args) {

SortB mySortB = new SortB();
mySortB.sortIncrease(6);
mySortB.sortDecrease(6);
}
}

If anyone can offer some advice on how I might introduce a small routine to have it accept user input otherwise take the default it would be greatly appreciated.

Thanks very much for an advise.

Recommended Answers

All 12 Replies

Ask the user if he wants to fill the array manually or automatically using:

System.out.println("Would you like to enter manually of automatically?")

Then use if statements to generate the different results.
For example:

if(//user input == manually)
//use a for loop to accept values up to the size of the array
if( //user input == auto)
//fill the array as you normally would

I hope this is of some help to you.

By the way, as the first person to reply to this thread I feel it is my duty to tell you to use code tags when supplying us with code.
Thanks and good luck.

Member Avatar for gferrie

Thanks very much. This is my first post so will be more cognisant of the tags. I will play a bit more and see what I can come up with.

Thanks very much. This is my first post so will be more cognisant of the tags. I will play a bit more and see what I can come up with.

Ok. Feel free to ask any questions you encounter along the way.
Good luck!

When using scanner if you use "next()", the data is in String format and needs to be converted. If you use, "nextInt" or "nextDouble", it converts the data for you automatically--consider using "hasNextInt()" or "hasNextDouble()" as well. Here's something to get you started. Create a new scanner.

Scanner in = new Scanner(System.in);

If you read data using, "next()", to convert to an Integer, use "Integer.parseInt(myStringData)".

Member Avatar for gferrie

So this is my 2nd stab at this and still working out the bugs:

public class Sort extends SortB  {

private static int sortIncrease;
private static int sortDecrease;

    /** 
     * Main method used here as point of entry to application.
     *
     * @param args array of String arguments
     */
    static public void main(String[] args) {
        if (args.length < 1 || args.length > 1) {
        System.out.println("Would you like to enter a random number " +
            "manually or accept the default of 6?" );
            System.out.println();
        } else if (args.length == 1) {
            sortIncrease = Integer.parseInt(args[0]);
            sortDecrease = Integer.parseInt(args[0]);
        }

        SortB mySortB = new SortB();
        mySortB.sortIncrease(6);
        mySortB.sortDecrease(6);
    }
}

Am I on the right track here or completely off base? I am also playing with a variation using the Scanner object as well but haven't had much luck yet.

Thanks for that advise

System.out.println("Would you like to enter a random number " +
            "manually or accept the default of 6?" );

I don't understand this part of your code. How do you expect the user to enter random values? Do you want the user to input values that he makes up or do want the computer to randomly generate numbers? Can you please clarify this.

Member Avatar for gferrie

What I would like the program to do is allow for the user to inpute an argument in place of the default (6) which gives the number of random numbers to generate. So ideally it could be done one of two ways. The easiest would just be to allow for the user to run the program with the following syntax:

java Sort

and it would use the default argument of (6).

Alternately the users could enter an argument on their own from the command line:

java Sort "10"

which would then generate 10 random numbers to sort for both sortIncrease and sortDecrease.

It doesn't have to do anything fancy at all just allow for the user to do provide an alternative argument of some other random number. It doesn't even need to prompt them for this. I could include instructions in a readme file which would explain the user input.

Does that make more sense?

Thats much clearer. Thanks.
I would user the scanner object to obtain user input and then apply that number to the size of the array.
use:

Scanner input = new Scanner(System.in);
int number = input.nextInt();

The number input by the user will be stored in the variable 'number'.

Member Avatar for gferrie

Thanks for all the help! in the end this is what I came up with and it seems to do the job.

if (args.length == 0) {
            mySortB.sortIncrease(6);
            mySortB.sortDecrease(6);
        }
        else if ( args.length == 1 ) {
            mySortB.sortIncrease(Integer.parseInt(args[0]));   
            mySortB.sortDecrease(Integer.parseInt(args[0]));
        }
        else {
            System.out.println("Too many arguments. Use the " +
                "syntax: java Sort \"10\"");   
        }
    }

really appreciate pointing me in the right direction.

Cheers

No problem, that's what we are here for.
Also, please mark the thread as solved if your are satisfied with your answer.

Member Avatar for gferrie

This has been solved thanks to the help I received!

G

This has been solved thanks to the help I received!

LOL. Thats not what I meant. Go to the bottom of the page with the last post and click the link that says Mark as solved (or something to that affect.)
Thanks again G. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.