Code-Please help to solve this

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.

i have done the following 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(“nothing”)

i am getting type mismatch error in 3rd line , Please assist how to correct.

First, please format your code.

Second, try reading and debugging the code yourself. You have many typos in your code, which give the impression that you are not even trying to solve the problem.

def convertToInt(input:String): Option[Int] = input

If input is a String and you have to return an Option[Int], how did you expect just returning the input would work? That is the type mismatch error you are seeing, and basically this is the exercise you have to solve.

case Some(input) => println(convertToInt)

Why do you want to print convertToInt which is a function? Which you already called?
So maybe, you actually wanted to print the input inside the Some.

Also, I would recommend you to use different names instead of input everywhere so it makes easier to debug the code.

I would love to help you, but I’m not sure what kind of help would be appropriate.

It appears you are taking some kind of class (or lab, or whatever you call it), where they gave you some homework. Then you used Google Translate (or something similar) to translate the problem statement to English and posted it here. Somehow, the distinction between number and digit got lost in translation.

The code you posted not only has a type mismatch. The code is nowhere near solving the stated problem.

You might be able to find someone who gives you a working code, but you will not learn anything by doing so.

What are you hoping to achieve? Do you want to learn Scala? Or you just need to pass this class because someone told you you need to pass it?

1 Like

I am solving problem on scala and learning that as well.If somebody ask something on community,that does not mean that he/she do not want to learn .On what basis you are saying that i Am translating the text ?

I asked for the help , who has interest for helping me , not just giving lecture.
I do understand that nobody learns by posting the code asking.

Instead you could have provided some suggestion to solve the code.I think you don’t know except giving lecture.

Thank you for your kind help.I do not need help from you.

Thank you sir, I will revert you back after sometime if I found some doubts.