import java.text.*; import java.util.Locale; public class Numbers { public static void main(String args[]) { // Initialize total double answer = 0; // Working variable double number; // Loop through all the command line args for (int i=0; i < args.length; i++) { try { // First try to convert arg to an int number = Integer.parseInt(args[i]); // Add to total if int answer += number; } catch (NumberFormatException intTry) { try { // Next try to convert arg to a double number = Double.parseDouble(args[i]); // Multiple to total if double answer *= number; } catch (NumberFormatException doubleTry) { // Display message if neither int nor double System.err.println("Arg: " + (i+1) + " invalid (" + args[i] + ")"); } } } // Create a currency display format with Local included NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); // Display output as US currency format System.out.println("The answer is " + nf.format(answer)); } }