-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineMaxFinder.java
More file actions
43 lines (38 loc) · 1.75 KB
/
Copy pathCommandLineMaxFinder.java
File metadata and controls
43 lines (38 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class CommandLineMaxFinder {
public static void main(String[] args) {
// Check if no arguments were passed at all
if (args.length == 0) {
System.out.println("Error: No arguments provided. Please provide at least one integer.");
System.out.println("Usage Example: java CommandLineMaxFinder 10 25 5");
return;
}
int max = Integer.MIN_VALUE;
boolean foundValidInt = false;
System.out.println("--- Processing Command Line Arguments ---");
// Iterate through all provided arguments
for (String arg : args) {
try {
// Attempt to parse the string argument into an integer
int currentNum = Integer.parseInt(arg);
// If it's the first valid integer found, it becomes our initial max
if (!foundValidInt) {
max = currentNum;
foundValidInt = true;
} else if (currentNum > max) {
// Update max if the current number is larger
max = currentNum;
}
} catch (NumberFormatException e) {
// Handle the case where the user typed a word or symbol instead of a number
System.out.println("Warning: '" + arg + "' is not a valid integer and will be ignored.");
}
}
// Final output based on whether valid integers were found
System.out.println("-----------------------------------------");
if (foundValidInt) {
System.out.println("The largest integer provided is: " + max);
} else {
System.out.println("Error: No valid integers were provided in the arguments.");
}
}
}