Pattern Match Scala

Question :
PatternMatchingType takes an integer input from command line. Based on the input, write a code for two match cases. In the first case based on the input1 from command line, convert initial to integer if input is 1 , double if input is 2 , and ‘String’ for any other input, and assign it to change . In the second match case based on type of change, print Integer is given if integer , Double is given if double and String is given if String .

Code

object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
//write your code here
println(change.getClass)
}
}

I tried below code but not working
object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case a if input1== 1 => input1.asInstanceOf[Int]
case b if input1 == 2 => input1.asInstanceOf[Double]
case c if input 1== 3 => input1.asInstanceOf[String]
println(change.getClass)
}
}

Please advise on above code

Hi, first as an advice, you can format your code like this:

```scala
// Your code here.
```

Second, asInstanceOf doesn’t really do anything. It just tells the compiler to shut up and believe what you say.
For example, imagine a situation like this:

val anAny: Any = 10
va anInt: Int = anAny.asInstanceOf[Int]

Here the compiler would have rejected the program, since an Any can not be (safely) passed when an Int is expected. But the asInstanceOf just forces the compiler to believe.
In this particular case, it would work in runtime, since actually anAny is an instance of Int, but if it would have been any other thing, it would have failed (as you have already seen).

In general, asInstanceOf is very unsafe and usually useless, especially for a beginner. My advice is to forget it exists, if you are in a situation when you need it, then you have a design problem.

Now, returning to your question. Your intuition is good, but you need a real transformation function.
You can find them on the documentation.

PS: The idea of pattern matching is match a pattern.
case a if input1== 1 => is redundant, you can just case 1 => :wink:

Problem Statement

PatternMatchingType takes an integer input from command line. Based on the input, write a code for two match cases. In the first case based on the input1 from command line, convert initial to integer if input is 1 , double if input is 2 , and ‘String’ for any other input, and assign it to change . In the second match case based on type of change, print Integer is given if integer , Double is given if double and String is given if String .

Code Tried

object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case 1 => println(“Integer” , +initial.toInt)

println(change.getClass)
}
}}
Expected Output :
String is given
Class java .lang.String

It is not working

Please advise

Here is how you want to structure your code

val change = input1 match {
  // todo insert your match statements here
}
change match {
  // todo insert your second match statements here
}

you should try and avoid var when writing functional code

I am trying with below code but not working . Please advise
Problem Statement

PatternMatchingType takes an integer input from command line. Based on the input, write a code for two match cases. In the first case based on the input1 from command line, convert initial to integer if input is 1 , double if input is 2 , and ‘String’ for any other input, and assign it to change . In the second match case based on type of change, print Integer is given if integer , Double is given if double and String is given if String .

Code Tried
object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case 1 => initial.toInt
println (“Integer”)
println(change.getClass)
case 2 => initial.toDouble
println (“Double”)
println(change.getClass)
case 3 => initial.toString
println (“String”)
println(change.getClass)
}
}}

Expected Output

String is given
Class java .lang.String

Please advise

the problem there is that you are using a var but not reassigning it based on the input.

you would need to do change = initial.toInt in case 1. change = initial.toDouble in case 2 and so on. But as i said before this isn’t recommended in functional programming.

tried using val as well and initial.toInt
but not getting output as below
String is given
Class java .lang.String

perhaps its best if you go through some pattern matching tutorials

I could just give you the answer but that would rob you of the chance to discover it yourself. Here is the scala tour page related to pattern matching

https://docs.scala-lang.org/tour/pattern-matching.html

its worth saying as well that you should split out what you need to do. first of all take the input and create the changed value. Once you’ve done that focus on pattern matching on the changed value to generate the println output

ok. Let me try again

I have tried as advised and also got the required OUTPUT . But it is not getting marked as completed.

Please advise me as I have come till here . Again I am posting

Problem Statement

PatternMatchingType takes an integer input from command line. Based on the input, write a code for two match cases. In the first case based on the input1 from command line, convert initial to integer if input is 1 , double if input is 2 , and ‘String’ for any other input, and assign it to change . In the second match case based on type of change, print Integer is given if integer , Double is given if double and String is given if String .

Expected Output

String is given
Class java .lang.String

Code I tried

object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case 1 => initial.toInt
println (“Integer”)
println(change.getClass)
case 2 => initial.toDouble
println (“Double”)
println(change.getClass)
case 3 => initial.toString
change ="String"
println (“String is given”)
println(change.getClass)
}
}}

Please advise

“case _” instead of “case 3” then it will work

Thanks a lot !! It worked :slight_smile:

It still not working the code which was posted earlier
object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case 1 => initial.toInt
println(“Integer is given”)
println(change.getClass)
case 2 => initial.toDouble
println(“Double is given”)
println(change.getClass)
case 3 => initial.toString
change =“String”
println(“String is given”)
println(change.getClass)
}
}
}

[quote=“sourav1311, post:13, topic:6046”]
object PatternMatchingType{
def main(args :Array[String]){
val input1 = args(0).toInt
val initial = “12345”
var change : Any = 0
input1 match {
case 1 => initial.toInt
println(“Integer is given”)
println(change.getClass)
case 2 => initial.toDouble
println(“Double is given”)
println(change.getClass)
case _ => initial.toString
change =“String”
println(“String is given”)
println(change.getClass)
}
}
}

this one is correct