Error illegal start of simple expression

Ruben Lejzerowicz

I am having this error :

error: illegal start of simple expression def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield (val a = line.split(",")) (a[0],a[4].toDouble)

for this line of code :

def process_alcs(lines: List[String]) : List[(String, Double)] = 
  for (line <- lines) yield (val a = line.split(",")) (a[0],a[4].toDouble)

I first thought it was a parenthese mismatch but I checked 100 times

rmathews7

Try

 def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield {
   val a = line.split(",")
   (a(0),a(4).toDouble)
 }

Basically, you are performing a multi line evaluation in your yield. As such, you must encapsulate the code in it with { instead of (. Also, both statements should be encapsulated in a single {} pair since they build up to what you want to yield.

Additionally, array members in scala are accessed using () and not []. So, you want to use a(0) instead of a[0]

On a separate note, this seems related to another question someone else asked today in which case he wanted to convert to double the value in the 4th column. To access the 4th column, you would want to use a(3).

In that case, the code becomes,

def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield {
  val a = line.split(",")
  (a(0), a(3).toDouble)
}

Hope this helps. Let me know if you have any further questions :)

Edit: To convert to map, try,

def process_as_map(lines: List[String]) : Map[String, Double] = {
  val results: List[(String, Double)] = for (line <- lines) yield {
    val a = line.split(",")
    (a(0), a(3).toDouble)
  }
  results.toMap
}

Refer comments for explanation, on why I tweaked as such based on your code

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I keep getting an "illegal start of expression" error on my code

error: illegal start of expression in Android Studio Code

Java Error: illegal start of expression

Illegal start of expression for Annotations

javac strange syntax - error illegal start of expression

Compile Error: illegal start of expression

Why is this an illegal start of expression?

Receiving Illegal start of expression error

Illegal Start of Expression, trying to print

Illegal start of expression error in Java

Illegal start of expression (method)

illegal start of simple expression and dont know why?

What's wrong with my java program, "illegal start of expression error"

Illegal Start of Simple Expression: Option Type and Ellipsis

Netbeans is giving me "illegal start of expression" error message on my methods

'Try without catch error', ')' expected, illegal start of expression..?

Weird "illegal start of expression" error in Netbeans

java illegal start of expression error

java error in for loop condition evaluation: not a statement, illegal start of expression

"Illegal start of expression" error in Java

How to fix illegal start of expression compilation error?

Error code compiling: illegal start of expression

Illegal Start of Simple Expression in Scala (Fibonacci Function)

Error: illegal start of expression followed by PriorityQueue

illegal start of simple pattern about for expression in scala

Running into an illegal start of expression error while changing the value of an array

error: illegal start of expression in my Android studio code

Error in code, "illegal start of expression"

Illegal start of simple expression creating a simple dataframe in spark