1. import java.util.Arrays;
  2. public class ArgumentWrapper {
  3. private static final int MAX_VALUE_1 = 100;
  4. private static final int MIN_VALUE_1 = 0;
  5. private static final int MAX_VALUE_2 = 50;
  6. private static final int MIN_VALUE_2 = -10;
  7. public static int processArgument(String[] args) {
  8. // Process the first argument
  9. int arg1 = -1;
  10. if (args.length > 0) {
  11. try {
  12. arg1 = Integer.parseInt(args[0]);
  13. if (arg1 < MIN_VALUE_1 || arg1 > MAX_VALUE_1) {
  14. System.err.println("Error: Argument 1 out of range. Must be between " + MIN_VALUE_1 + " and " + MAX_VALUE_1);
  15. return -1; // Indicate an error
  16. }
  17. } catch (NumberFormatException e) {
  18. System.err.println("Error: Argument 1 must be an integer.");
  19. return -1; // Indicate an error
  20. }
  21. }
  22. // Process the second argument
  23. int arg2 = -1;
  24. if (args.length > 1) {
  25. try {
  26. arg2 = Integer.parseInt(args[1]);
  27. if (arg2 < MIN_VALUE_2 || arg2 > MAX_VALUE_2) {
  28. System.err.println("Error: Argument 2 out of range. Must be between " + MIN_VALUE_2 + " and " + MAX_VALUE_2);
  29. return -1; // Indicate an error
  30. }
  31. } catch (NumberFormatException e) {
  32. System.err.println("Error: Argument 2 must be an integer.");
  33. return -1; // Indicate an error
  34. }
  35. }
  36. // Combine or use the processed arguments (example: sum them)
  37. if (arg1 != -1 && arg2 != -1) {
  38. return arg1 + arg2;
  39. } else if (arg1 != -1) {
  40. return arg1;
  41. } else if (arg2 != -1) {
  42. return arg2;
  43. }
  44. else {
  45. return 0; // Default value if no arguments provided
  46. }
  47. }
  48. public static void main(String[] args) {
  49. // Example Usage
  50. int result = processArgument(args);
  51. System.out.println("Result: " + result);
  52. }
  53. }

Add your comment