object ValidNumberChecker { def isValidNumber(s: String): Boolean = { // Check if the string is empty or consists only of whitespace if (s.trim.isEmpty) return false // Remove the optional starting '+' or '-' val stringWithoutSign = if (s.startsWith("+") || s.startsWith("-")) s.substring(1) else s // Split the string at the decimal point and check both parts val parts = stringWithoutSign.split("\\.") parts.length match { case 1 => isAllDigits(parts(0)) case 2 => isAllDigits(parts(0)) && isAllDigits(parts(1)) case _ => false } } private def isAllDigits(s: String): Boolean = s.forall(_.isDigit) def main(args: Array[String]): Unit = { val testStrings = List("123", "-123", "+123", "123.456", "-123.456", "+123.456", "abc", "123.", ".456", "123..456") testStrings.foreach(s => println(s"$s is a valid number: ${isValidNumber(s)}")) } }