0

I am trying to create a method that takes a list of lists (which reads from a csv file), and a string, and outputs a list of all the data that has that string.

Data example:

"Joe, 14,1989"
"John, 13,1999"
"Mary, 10, 2000"

the function I have is this:

def name(info: List[List[String]], str: String): List[List[String]] = 
info.filter(lst =>lst.head.toString == str)

However, I get an empty list in return. What is the best way to compare strings in scala?

1
  • Is info.head supposed to be List("joe","14","1989")? b/c yes "joe" == "joe" in scala. Commented Feb 10, 2016 at 0:03

1 Answer 1

2

Since you're calling .head, this will return only lists whose first element is equal to str. (It's also superfluous to call .toString, since the items are already strings.)

What you want is info.filter(lst => lst.contains(str)), or simply info.filter(_.contains(str)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.