1

Hi I want to use a "for" into a map method in scala.

How can I do it?

For example here for each line read I want to generate a random word :

val rdd = file.map(line => (line,{
            val chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            val word = new String;
            val res = new String;
            val rnd = new Random;
            val len = 4 + rnd.nextInt((6-4)+1);

            for(i <- 1 to len){
                val char = chars(rnd.nextInt(51));
                word.concat(char.toString);
            }

            word;
}))

My current output is :

Array[(String, String)] = Array((1,""), (2,""), (3,""), (4,""), (5,""), (6,""), (7,""), (8,""), (9,""), (10,""), (11,""), (12,""), (13,""), (14,""), (15,""), (16,""), (17,""), (18,""), (19,""), (20,""), (21,""), (22,""), (23,""), (24,""), (25,""), (26,""), (27,""), (28,""), (29,""), (30,""), (31,""), (32,""), (33,""), (34,""), (35,""), (36,""), (37,""), (38,""), (39,""), (40,""), (41,""), (42,""), (43,""), (44,""), (45,""), (46,""), (47,""), (48,""), (49,""), (50,""), (51,""), (52,""), (53,""), (54,""), (55,""), (56,""), (57,""), (58,""), (59,""), (60,""), (61,""), (62,""), (63,""), (64,""), (65,""), (66,""), (67,""), (68,""), (69,""), (70,""), (71,""), (72,""), (73,""), (74,""), (75,""), (76,""), (77,""), (78,""), (79,""), (80,""), (81,""), (82,""), (83,""), (84,""), (85,""), (86...

I don't know why the right side is empty.

1
  • I have the same problem but in my case i already declared them as var but still didn't work Commented Mar 13, 2018 at 9:07

2 Answers 2

2

There's no need for var here. It's a one liner

  Seq.fill(len)(chars(rnd.nextInt(51))).mkString

This will create a sequence of Char of length len by repeatedly calling chars(rnd.nextInt(51)), then makes it into a String.

Thus you'll get something like this :

import org.apache.spark.rdd.RDD
import scala.util.Random

val chars = ('a' to 'z') ++ ('A' to 'Z')

val rdd = file.map(line => {
  val randomWord = {
    val rnd = new Random
    val len = 4 + rnd.nextInt((6 - 4) + 1)
    Seq.fill(len)(chars(rnd.nextInt(chars.length-1))).mkString
  }
  (line, randomWord)
})
Sign up to request clarification or add additional context in comments.

2 Comments

I've took the liberty to update your answer if you don't mind. :)
If you like. I thought it was fairly clear already :) I tweaked it still further
1

word.concat doesn't modify word but return a new String, you can make word a variable and add new string to it:

var word = new String
....
for {
    ...
    word += char
    ...
}

2 Comments

when I try it i receive this error error: value += is not a member of String word += char.toString
You need to declare word as a variable. Notice the var keyword in front of word instead of val which is immutable.

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.