Java programs must ALWAYS be run via the class name which has the main method. The arguments to the main method are the command line arguments. If you only have one class - the Circles class, then change your existing constructor into the main method. Here's a short example:
Code:
class Circles{
public static void main(String[] args){
if(args.length != 1){ //If there are no command line arguments, or too many arguments
System.out.println("Usage: java Circles <number>.");
System.exit(0); //Exit the program
}
else{
//method to parse the character in args[0] into an integer, then to draw the circles
}
}
}
It's important to remember that command line arguments are passed as strings. If you want to parse the command line argument into an integer, you need to use:
Code:
try{
int myInt = Integer.parseInt(myString);
}
catch(NumberFormatException nfe){
System.out.println("Couldn't convert command line arguments into an integer!);
}
The Try/Catch code is there because the conversion of the string into an integer might fail. Without the try/catch, your program would simply crash. This could happen if the user tries to run your program like this, for example:
instead of
Because Java doesn't know how to turn 'five' into 5, but knows how to turn the CHARACTER '5' into the number 5.
Once your code is correct, you compile it like this:
and run it like this:
Code:
java Circles <number>
where number is the number of circles you wish to draw. Remember you DON'T use:
Code:
java Circles.class <number>
because this confuses the Java program, and causes an error. You must leave off the .class extension.
If you have any more problems, just ask here, we'll be glad to help
Bookmarks