Reputation: 2305
I'm simply trying to get an Int value that's associated with a String key. I get:
error: type mismatch;
found : Char
required: String
score += tiles(letter)
from my code here:
val tiles = Map[String, Int](
"a" -> 1,
"b" -> 3,
"c" -> 3,
"d" -> 2
// etc.
)
def main(args: Array[String]) {
println("\nScrabble Calculator 1.0")
println("Enter words on the commandline.")
println("Use a '_' character for blank tiles.\n")
for (w <- args) // loop through each word
if (w.length < 2)
println(w + ": one-letter words disallowed in Scrabble")
else
calculate(w)
}
def calculate(w: String) {
var score = 0
for (letter <- w)
score += tiles(letter)
println(w + ": " + score + " points")
}
If I do "a" or "b" instead of letter
, it works fine (it returns 1, or 3, or whatever).
Upvotes: 0
Views: 1493
Reputation: 71485
tiles
is a Map[String, Int]
.
calculate
takes a String
argument, w
. It pulls individual characters out of w
into letter
, so letter
is of type Char
.
You then try to look up letter
in tiles
. letter
is a Char
, when tiles
needs a String
. This is precisely what the compiler is telling you with that error message.
Upvotes: 1
Reputation: 2305
Oh, jeez. It was a simple syntax problem. If you use ' instead of ", it is interpreted as a Char:
val tiles = Map[Char, Int](
'a' -> 1,
'b' -> 3,
'c' -> 3,
'd' -> 2,
Upvotes: 1