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

Thanks below code is working

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)")
}
}

scalac Options.scala

Options.scala:9: warning: match may not be exhaustive.

It would fail on the following input: None

convertToInt(input) match - getting the above error - Can you help pls

It means exactly what it says: you have a case for Some, but you don’t have a case for None. That’s usually a bug, which is why the compiler is giving you a warning. You need to add a case that handles None, however makes sense for your needs…

u can ignore it