4
4
* Main application class implementing a snack distribution algorithm.
5
5
* When a child has a favorite snack number, we "flip" it by incrementing it by 1.
6
6
* If the incremented value exceeds k (the maximum snack number), we reset it to 1.
7
- * Handles user input processing and manages snack quantity increments .
7
+ * Exits silently on any invalid input without error messages .
8
8
*/
9
9
public class App {
10
10
/**
11
11
* Main entry point of the application.
12
12
* Processes user input and coordinates snack distribution logic.
13
- * Reads input, processes snack numbers according to requirements, and displays the result .
13
+ * Exits silently on any invalid input .
14
14
* @param args Command line arguments (not used)
15
- * @throws Exception If input processing fails
16
15
*/
17
- public static void main (String [] args ) throws Exception {
18
- boolean looper = true ; // Controls main program loop
16
+ public static void main (String [] args ) {
17
+ Scanner userInput = new Scanner ( System . in );
19
18
20
- while (looper ) {
19
+ try {
20
+ // Get initial values (n and k)
21
+ String firstLine = userInput .nextLine ();
22
+ String [] parts = firstLine .split ("\\ s+" );
23
+
24
+ // Exit silently if format is invalid
25
+ if (parts .length != 2 ) {
26
+ return ;
27
+ }
28
+
29
+ // Parse input values and exit silently on any parsing error
30
+ int n , k ;
21
31
try {
22
- int k = 0 ; // Maximum snack number limit
23
- int n = 0 ; // Number of children
24
- String stringInput = "" ; // Temporary storage for input
25
-
26
- // Initialize arrays for tracking snacks
27
- int [] a = new int [40 ]; // Stores initial snack quantities
28
- int [] snack = new int [40 ]; // Tracks updated snack counts
29
-
30
- Scanner userInput = new Scanner (System .in );
31
-
32
- // Get initial values (n and k)
33
- while (true ) {
34
- stringInput = userInput .nextLine ();
35
- String [] parts = stringInput .split ("\\ s+" );
36
-
37
- // Parse input values
38
- n = Integer .parseInt (parts [0 ]); // Number of children
39
- k = Integer .parseInt (parts [1 ]); // Maximum snack limit
40
-
41
- boolean invalidInput = false ; // Track invalid input
42
-
43
- // Validate input ranges
44
- if (!(n <= 100 && n >= 1 )) {
45
- System .out .println ("Invalid Input for n, try again" );
46
- invalidInput = true ;
47
- }
48
-
49
- if (!(k <= 100 && k >= 1 )) {
50
- System .out .println ("Invalid Input for k, try again" );
51
- invalidInput = true ;
52
- }
32
+ n = Integer .parseInt (parts [0 ]); // Number of children
33
+ k = Integer .parseInt (parts [1 ]); // Maximum snack limit
34
+ } catch (NumberFormatException e ) {
35
+ return ;
36
+ }
37
+
38
+ // Validate input ranges silently
39
+ if (n < 1 || n > 100 || k < 1 || k > 100 ) {
40
+ return ;
41
+ }
42
+
43
+ // Process second line of input (individual snack quantities)
44
+ String secondLine = userInput .nextLine ();
45
+ String [] snackValues = secondLine .split ("\\ s+" );
46
+
47
+ // Exit silently if input count doesn't match expected
48
+ if (snackValues .length != n ) {
49
+ return ;
50
+ }
51
+
52
+ int [] snacks = new int [n ];
53
+
54
+ // Load and validate snack values silently
55
+ for (int i = 0 ; i < n ; i ++) {
56
+ try {
57
+ snacks [i ] = Integer .parseInt (snackValues [i ]);
53
58
54
- // Exit loop if both inputs are valid
55
- if (! invalidInput ) {
56
- break ;
59
+ // Exit silently if value is out of range
60
+ if (snacks [ i ] < 1 || snacks [ i ] > k ) {
61
+ return ;
57
62
}
58
- }
59
-
60
- // Process second line of input (individual snack quantities)
61
- stringInput = userInput .nextLine ();
62
- String [] parts2 = stringInput .split ("\\ s+" );
63
-
64
- boolean hasInvalidInput = false ; // Track invalid input
65
-
66
- // Load initial snack values
67
- for (int i = 0 ; i < n ; i ++) {
68
- try {
69
- a [i ] = Integer .parseInt (parts2 [i ]);
70
-
71
- // Throw if out of range (1 to k)
72
- if (a [i ] < 1 || a [i ] > k ) {
73
- throw new IllegalArgumentException ("Invalid Input for a[" + (i + 1 ) + "], must be between 1 and " + k );
74
- }
75
- } catch (IllegalArgumentException e ) {
76
- if (!hasInvalidInput ) {
77
- System .out .println (); // Add space when first error occurs
78
- }
79
- System .out .println (e .getMessage ()); // Prints range error
80
- hasInvalidInput = true ;
81
- }
82
- }
83
-
84
- // Exit program if any invalid input was found
85
- if (hasInvalidInput ) {
63
+ } catch (NumberFormatException e ) {
86
64
return ;
87
65
}
88
-
89
- // Filter snacks within valid range (1 to k)
90
- for (int i = 0 ; i < n ; i ++) {
91
- if (a [i ] >= 1 && a [i ] <= k ) {
92
- snack [i ] = a [i ];
93
- }
94
- }
95
-
96
- // Process the "flip" operation: increment each snack number or reset to 1 if exceeds k
97
- for (int i = 0 ; i < n ; i ++) {
98
- if (snack [i ] >= k ) {
99
- snack [i ] = 1 ; // Reset to minimum if exceeds maximum
100
- } else {
101
- snack [i ] += 1 ; // Otherwise increment normally
102
- }
66
+ }
67
+
68
+ // Process the "flip" operation: increment each snack number or reset to 1 if equals k
69
+ for (int i = 0 ; i < n ; i ++) {
70
+ if (snacks [i ] == k ) {
71
+ snacks [i ] = 1 ; // Reset to 1 if exactly equals maximum
72
+ } else {
73
+ snacks [i ]++; // Otherwise increment normally
103
74
}
104
-
105
- // Output the result
106
- System .out .println (); // Add space before output only if there was an error
107
- for (int i = 0 ; i < n ; i ++) {
108
- System .out .print (snack [i ] + " " );
75
+ }
76
+
77
+ // Output the result
78
+ StringBuilder result = new StringBuilder ();
79
+ for (int i = 0 ; i < n ; i ++) {
80
+ result .append (snacks [i ]);
81
+ if (i < n - 1 ) {
82
+ result .append (" " );
109
83
}
110
- System .out .println ();
111
-
112
- looper = false ; // Exit main loop
113
- } catch (Exception e ) {
114
- // Handle invalid input or processing errors
115
- System .out .println ("\n Error: " + e );
116
- System .out .println ("Make sure your inputs are correct. Repeating program...\n " );
117
84
}
85
+ System .out .println (result .toString ());
86
+
87
+ } catch (Exception e ) {
88
+ // Silently exit on any exception
89
+ return ;
90
+ } finally {
91
+ userInput .close ();
118
92
}
119
93
}
120
- }
94
+ }
0 commit comments