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

We had given below task

Write the function converttoInt which uses the Option return of Integer . In case all the characters in the input are integers, convert the input to integer and return it. Else, return None. In the second task after converting the input, use match to print the return value and type of the return value from convertToInt function.

We need to wriite code in below but not getting how to write code to resolve above task, please help us


object Options extends App {
val input = args(0)
def convertToInt //complete the code
convertToInt(input) match //complete the code
}

Output will be:
(100, int)

Below are some excerpts from an ammonite session that should help you piece together the first part.

@ '3'.isDigit 
res0: Boolean = true

@ "342".forall(_.isDigit) 
res1: Boolean = true

@ "342".toInt 
res2: Int = 342

@ "342a".forall(_.isDigit) 
res3: Boolean = false

@ Option("342".toInt) 
res4: Option[Int] = Some(342)

And for the second part

@ val s : Option[Int] = Some(4) 

s: Option[Int] = Some(4)
@ s match {
       case Some(x) => println(s"I got an integer $x")
       case None => println("I should be printing nothing here")
  } 
I got an integer 4

regards,
Siddhartha

We need out as (100, int), how to get it.

Written code as


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

Not working, getting below error -

$ scalac Options.scala
Options.scala:4: error: type mismatch;
found : String
required: Int
val s: Option[Int] = Some(input)
^
Options.scala:5: error: type mismatch;
found : String
required: Int
convertToInt(input) s match {
^
Options.scala:6: 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)
^
three errors found

Have you worked through the examples that @siddhartha-gadgil has provided? Are there any you do not understand?

Yes we worked on it and tried to developed code as per our requirement but getting above issue

Note that you did not use isDigit

Can u please let us know where we need to use isdigit() option in below code -


object Options extends App {

val input = args(0)
def convertToInt(x: Int) = input.toInt
val s : Option[Int] = Some(input)
convertToInt(input) s match {
case Some(input) => println(convertToInt)
case None => println(“I should be printing nothing here”)
}
}

Have you tried incorporating isDigit yourself? What did you try? Where did you get stuck?