public class CommandLineSum {
public static void main(String[] args) {
int sum = 0;
// Check if there are command line arguments
if (args.length == 0) {
System.out.println("No command line arguments found.");
return;
}
// Iterate through the command line arguments
for (String arg : args) {
try {
// Convert argument to integer and add to sum
int num = Integer.parseInt(arg);
sum += num;
} catch (NumberFormatException e) {
System.out.println(arg + " is not a valid integer.");
}
}
// Print the sum of integers
System.out.println("Sum of integers: " + sum);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter