Kotlinlearncs.online LogoJava
Return to List

Test Writing: Count Valid Strings

Created By: Rohith Sanjay
/ Version: 2024.9.1

Write a method countValidStrings that accepts one argument: a List<String> called list. Each String in the list contains parts separated by colons (:). Your method should count and return (as an int) the number of strings in the list where the value immediately after the first colon, after trimming whitespace, equals "valid" (case-insensitive).

For example, "CS124: valid" is counted because the part after the first colon is " valid", which after trimming equals "valid". "CS124: VALID" is also counted because the comparison is case-insensitive. "CS124: invalid" is not counted because "invalid" is not equal to "valid".

Note that only the part immediately after the first colon matters. For example, "aa: bb: valid" is not counted because the part after the first colon is " bb", not "valid". However, "aa: valid: bb" is counted because the part after the first colon is " valid".

You may use java.util.List without importing it.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Java's assert method
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/java/count-valid-strings/[email protected]