I need to write a Java class called ArgStats. This should process all the command line arguments and do the following. 1.If there are no arguments, or one or more are not integers, it should print the message: “Please supply one or more integers; only integers are allowed”
2.If all arguments are integers, it should print the number of arguments, the total, and the average.
Note: the command line arguments are passed to the main method as args, given the method signature public static void main (String[] args). I tried to do this, but I need help in some parts of 1st statement.And I dont know how to calculate and show number of arguments in 2nd statement.
My java code is this:
import java.util.Scanner;
public class ArgStats {
public static void main(String[] args) { Scanner S = new Scanner(System.in);
int tot = 0; int avrg = 0; while (S.hasNextDouble()) { double x = S.nextDouble(); tot +=x; avrg++; System.out.println("read: " + x); }
int length = args.length; if (length <= 0) {
System.out.println("Please supply one or more integers; only integers are allowed");
public class ArgStats { static int total=0; static double average; static int i;
public static void main(String[] args) {
if (args.length == 0) { System.out.println("Please supply one or more integers; only integers are allowed");
} else{ for (i = 0; i < args.length; i++) { try { total += Integer.parseInt(args[ i ].trim()); } //TAKE OUT THE SPACES IN [ I ] I had to write it this way for the forum catch (NumberFormatException nfe){ System.out.println("Please supply one or more integers; only integers are allowed"); System.exit(1); } } average = ((double)total/i); System.out.println("Number of Arguments = " + i + "\nTotal = " + total + "\nAverage = " + average); } } }
Do you know how to compile a program from the command line with arguments? To run this program type "java ArgStats 5 12 4520 356" (or whatever numbers you want) in the command line. No need for scanner unless you want to take the arguments one at a time.
You need to run it from the command line, not an IDE. open cmd.exe on your computer. Move to the directory where you saved ArgStats.java using the commands cd (changedirectory) and dir (directory) to see whats in the current level. type: "javac ArgStats.java" to compile. Then run it with "java ArgStats <args>". For example: "java ArgStats 10 50 248 51"
But does it mean that my codes were wrong? because I need to run and test in in Java IntelliJ.
and when I try the way you say I get error in command line,I do exactly as youtube video, but when I write "javac ArgStats.java" to compile it, I get this error: "javac" is not recognized as an internal or external command,operable program or batch file.
Try to pass the arguments while running the program like as follows
right click on the program and u can see Run As--> Runconfigurations click on this then it will open a window in that window there is a tab Arguments click on that it will open a rich text box there provide the command line arguments and press apply button and click on run then the program will run as u expected.