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?

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 for us

“Not working”: you get an error message, or unexpected behavior, or what? Please include the exact error message or describe the unexpected behavior.

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

@ashishsme14

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

we writing 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”)
}
}

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.

In that case the typ signature of convertToInt should be the following def convertToInt(input : String) : Option[Int] .

Once you have that you can call that function and pattern match on the result:

convertToInt(args(0)) match {
  ...
}

Tried now below code -

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

https://docs.scala-lang.org/tour/basics.html at the section “methods” explains the syntax of methods. You didn’t get it completely right.

written code as -

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.