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
}
@ 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
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
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”)
}
}
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”: you get an error message, or unexpected behavior, or what? Please include the exact error message or describe the unexpected behavior.
$ 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
I think rather than just stating that its not working you should read your code and you will see what is wrong. Type declarations especially for methods are good for this.
val input = args(0) input is a string since args is an array of string
def convertToInt(x: Int) = input.toInt the current return type is an Int is that your intent?
val s : Option[Int] = Some(input) your are binding Some[Int] to Option[String] this obviously wont compile
convertToInt(input) s match { its unclear what you are trying to do here
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”)
}
}
to get
Output will be:
(100, int)
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.
object Options extends App {
val input = args(0)
def convertToInt(input:String)=Option[Int]
convertToInt(args(0)) match {
case Some(input) => println(convertToInt)
case None => println(“I should be printing nothing here”)
}
}
getting below error
$ scalac Options.scala
Options.scala:3: error: missing arguments for method apply in object Option;
follow this method with _' if you want to treat it as a partially applied function def convertToInt(input:String)=Option[Int] ^ 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
object Options extends App {
val input = args(0)
def convertToInt(input:String):Option[Int]
convertToInt(input) match {
case Some(input) => println(convertToInt)
case None => println(“I should be printing nothing here”)
}
}
Now getting -
Options.scala:3: error: only classes can have declared but undefined members
def convertToInt(input:String):Option[Int]
^
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
Yeah so now you have a method declaration you need to write the implementation. If you unsure about the implementation and want to make sure the rest of your program typechecks you can do def convertToInt(input: String) : Option[Int] = ??? but at some point you will need to implement the function
Not quite right yet: the implementation of convertToInt is missing.
The error is telling you that:
only classes can have declared but undefined members
def convertToInt(input:String):Option[Int]
^
It’s showing you that the member convertToInt is declared, but not yet defined (implemented)
The example in the tour is def add(x: Int, y: Int): Int = x + y. You have the left side of that, def add(x: Int, y: Int): Int is the part where you have def convertToInt(input: String): Option[Int] but not the right side, = x + y. Obviously the implementation you’re looking for is not x + y, but something with input.
I would really suggest following all examples in the tour basics page, and make sure you understand them.