Recap: Word Filter
Combine methods, return values, string methods, arrays, looping over arrays, and break/continue in a single mission
Mission: Radio Station Filter
Tommy is listening to the Vice City police radio. The feed is full of static, and at some point the operator signs off. Write a filter that cleans up the transmission and keeps only the real names.
Put the signals in a String array named signals, for example:
String[] signals = { "tommy", "static", "lance", "static", "cortez", "static", "diaz", "out", "mercedes" };
Write a method that takes the array of signals and returns a new array containing only the valid names, in uppercase. The method should:
- Loop through the array
- Skip any signal equal to
"static"(usecontinueand.equals()) - Stop completely when it hits
"out"(usebreak); everything after it is ignored - Turn the valid names with
.toUpperCase()and collect them into the result array
In main, call the method, print each name on its own line, then print how many names are left.
Tip: since you don’t know up front how many names will be valid, walk the array twice: first just count them, then create the result array of the right size and fill it on the second pass.
Example
For { "tommy", "static", "lance", "static", "cortez", "static", "diaz", "out", "mercedes" }
TOMMY
LANCE
CORTEZ
DIAZ
Total: 4
Example when the sign-off comes first { "out", "tommy", "lance" } (no valid names)
Total: 0
Example with no static and no sign-off { "tommy", "lance" }
TOMMY
LANCE
Total: 2