Andre Queen Andre Queen - 19 days ago 6
Scala Question

Scala Cannot Resolve Symbol

Currently my system has the ability for a user to enter 2 numbers, the last number on the tail is found and returned, how can I get the data and data2 to only show there numbers, not the SK1/SK3 before. Currently I am getting the error ;

Error:(177, 28) value >= is not a member of (String, Int)
val highest = if (data >= data2){

//Compare 2 values
def mnuCompareTwoStocks(f: (String) => (String, Int)) = {
print("Stock > ")
val data = f(readLine)
print("Stock 2 > ")
val data2 = f(readLine)
val highest = if (data >= data2){
println(data)
}
else
{
println(data2)
}
println(s"${data._1}: ${data._2} ${data2._1}: ${data2._2}")

}


EDIT:
The working outcome

//Compare 2 values
def mnuCompareTwoStocks(f: (String) => (String, Int)) = {
print("Stock > ")
val data = f(readLine)
print("Stock 2 > ")
val data2 = f(readLine)
if (data._2 >= data2._2){
println("The higher stock is " + data)
}
else
{
println("The higher stock is " + data2)
}
println(s"${data._1}: ${data._2} ${data2._1}: ${data2._2}")

}

Answer

You are trying to compare 2 tuples of type (String,Int). There is no simple comparison operator for such.

scala> ("a",3) > ("b",0)
<console>:12: error: value > is not a member of (String, Int)
       ("a",3) > ("b",0)
               ^

Do you want the String to take precedence or the Int?

scala> ("a",3)._1 > ("b",0)._1
res0: Boolean = false

scala> ("a",3)._2 > ("b",0)._2
res1: Boolean = true
Comments