-8

I wanted to get "a    e" in the below code by trying to mimic a ternary operator, but getting below error

scala> val ab="apple"
ab: String = apple

scala> ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0  )
res99: Array[Boolean] = Array(true, false, false, false, true)

scala> ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0 ? x : ' ' )
<console>:1: error: identifier expected but character literal found.
ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0 ? x : ' ' )
                                                       ^

scala>
stack0114106
  • 8,534
  • 3
  • 13
  • 38

1 Answers1

2

Valid Scala syntax is

ab.toCharArray.map(x => if ("aeiou".indexOf(x) >= 0) x else ' ')

On contrary

ab.chars().map(x -> "aeiou".indexOf(x) >= 0 ? x : ' ');

is Java syntax.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66