Can some help with Scala code for below Lab requirement

Write a function show using Option which returns the value of string . If inputCapital has a value show it must return the value, and if the value is None , return invalid . Print country name and capital as in the sample output format.

when executing with scala Options India .

Output will be: India : New Delhi

Need help to proceed with lab learning

Can you explain where you are getting stuck when you attempt to solve it yourself?

1 Like

object Options extends App {
val input = args(0)
val capitals = Map(“France” -> “Paris”, “Japan” -> “Tokyo”, “India” -> “New Delhi”, “Russia” -> “Moscow”)
val inputCapital = capitals.get(input)
println((“India” ) : + capitals.get( “India” ))

this code says
Options.scala:7: error: identifier expected but ‘.’ found.
println(“India”) : + capitals.get( “India” )

Hmm. I suspect the error message is misleading, and that the problem is actually that colon there. What do you expect ("India") : + capitals.get() to mean?

More generally: it’s a little hard to help you, because you aren’t formatting your code and it’s hard to read. Please, when you post code samples here, put them in code blocks. Before your code, put a line that contains just three backticks – just ``` on a line by itself – and then another one after your code. That’s absolutely essential to making your code readable, and is polite to the people trying to help. Thanks!

1 Like

We have written code as

object Options extends App {
 val input = args(0)
 val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo", "India" -> "New Delhi", "Russia" -> "Moscow")
 val inputCapital = capitals.get(input)
println(("India : ")  + capitals.get( "India" ))
}

getting output as

India : Some(New Delhi)

we need output as

India : New Delhi
US : invalid

please help us

We had modified code bit still not working

object Options extends App {
 val input = args(0)
 val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo", "India" -> "New Delhi", "Russia" -> "Moscow")
 val inputCapital = capitals.get(input)
println(input + " : "  + inputCapital)
}

Result as

$ scala Options India
India : Some(New Delhi)

$ scala Options US
US : None

We need as

India : New Delhi
US : invalid

Please help us

What does Map.get() return? Is it a String, or something else?

Written new code as

object Options extends App {
 val input = args(0)
 val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo", "India" -> "New Delhi", "Russia" -> "Moscow")
 val inputCapital = capitals.get(input)
 input match {
 case some(x) => println(x + " : "  + inputCapital)
 case None => println(x + " : invalid")
}
}

Now getting error as -

$ scalac Options.scala
Options.scala:6: error: not found: value some
case some(x) => println(x + " : " + inputCapital)
^
Options.scala:7: error: pattern type is incompatible with expected type;
found : None.type
required: String
case None => println(x + " : invalid")
^
Options.scala:7: error: not found: value x
case None => println(x + " : invalid")
^
three errors found

Thanks issue resolved correct code will be

object Options extends App {
 val input = args(0)
 val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo", "India" -> "New Delhi", "Russia" -> "Moscow")
 val inputCapital = capitals.get(input)
 inputCapital match {
 case Some(x) => println(input+ " : "  + (x))
 case None => println(input + " : invalid")
}
}