Need help in scala code not getting how to write required code

object Options extends App {
val input = args(0)
def convertToInt(input:String):Option[Int]=input
convertToInt(input) match {
case Some(input) => println(convertToInt)
case None => println(“I should be printing nothing here”)
}
}

Options.scala:3: error: type mismatch;
 found   : String
 required: Option[Int]
def convertToInt(input:String):Option[Int]  = input
                                              ^
Options.scala:5: error: missing arguments for method convertToInt in object Options;
follow this method with `_' if you want to treat it as a partially applied function
case Some(input) => println(convertToInt)
                            ^
two errors found

I think it’s a good idea to step back, stop fiddling with code, and start working through basic tutorials – what steps do you need to get to your result? What is it you need to do? If you can’t manage to do what you want, can you do something else, something simpler, make that work, and improve from there?

1 Like

Try below code:

object Options extends App {
val input = args(0)
def convertToInt(x: String): Option[Int] = {
val y = Option(x.toInt)
return y
// val z = x.toInt
// if (z.isDigit) return y
}
convertToInt(input) match {
case Some(x) => println("("+x+", Int)")
case None => println(“I should be printing nothing here”)
}
}

yes ,its working but please comment the case=>None.
Thank you