# Unable to debug the Issue

Problem Statement:
Generate a List with input1 such that the number of elements is equal to input1 , and the numbers are from 1 to input1 (when input is 3 -> List(1,2,3)). Write a function findEven using Option (container) such that it returns boolean true when the number is even, else returns None . Using the for loop, apply function findEven on each element of the list, and using match print if the number is even or odd.

Compile the program using: scalac Demo.scala .

Execute the program to print the outputs: scala Demo 3 .

Ouput will be: 1 is odd. 2 is even. 3 is odd.

I have written code as below and getting the output as expected.But unable to complete the exam.

object Demo extends App {
val input1 :Int = args(0).toInt
val numberList :List[Int]=(1 to input1).flatMap((x:Int)=>List(x)).toList
def findeven (x:Int):Boolean =
{
if (x%2==0)
{return true}
else
{return false}
}
for (n <- numberList) {
print(n)
if(findeven(n)) {
println(" is even.")
}
else{
println(" is odd.")
}
}
}

Welcome to Scala

Please be aware that we do not solve homework or exam exercises. However, if you have a specific question, we are happy to help.

Do you have a specific question?

(For future postings: Please format your source code using the ``` markers.)

1 Like

As @dubaut said, we wonâ€™t give you a solution, but I can give you a hint where to look.
Your code does produce the required output, but it doesnâ€™t do it in the way your problem statement requires.

Your findEven method does not use Option, as required. (also if this uses an automatic grading, youâ€™ll probably have to use the same capitalization for the function name as the exercise states).

You donâ€™t have any match in your code.

1 Like

Another quick note.

Your code does a lot of unnecessary things to built the list.
return is not only not necessary, but not recommended.

Also, are you sure you give us the complete definition of findEven? It is kind of weird to return an Option that can only be Some(true) or None. Also, it seems it has to be applied element by element of the list, so maybe it receives as input the index of the list and returns:
Some(true) if the element on that index is even.
Some(false) if the element on that index is odd.
None if the element on that index doesnâ€™t exists.

I also wondered that at first, but then concluded, that its probably meant to force them to use pattern matching. It still isnâ€™t how you would solve that problem in any real software project, of course.

How to use match in the code

You shoud first rewrite your findEven function to return an Option[Boolean], as the text suggests.
Then, you will match on the result of findEven, instead of using it in an if. An Option is always either a Some(a), with the value a stored in it or a None, with no value. See the pattern matching tutorial for more info.

PS: Your exercise demands the use of Option here, but note that this isnâ€™t a useful application of Option, you would only use it if you actually care about the possible content in a Some, not to replace a boolean.

1 Like

Thanks a TON to everyone for providing the resolution.I am able to clear the exercise.

1 Like

can you provide the correct code if you have saved somewhere;

This is the final code, Thanks all for help in explaining.
object Demo extends App {
val input1 :Int = args(0).toInt
val numberList :List[Int]=(1 to input1).flatMap((x:Int)=>List(x)).toList
def findeven (x:Int):Option[Boolean] =
{
if (x%2==0)
Some(true)
else
None
}
for (n <- numberList) {
print(n)
findeven(n) match {
case Some(true) => println(" is even.")
case None => println(" is odd.")
}
}
}

Can you help solve the issue -match may not be exhaustive.
It would fail on the following input: Some(true)
findeven(n) match

What do you have after the match â€“ really, what does your full code look like? The code right above your message should not get that particular error message, so I suspect you are doing something differentâ€¦