[Solved] How to get answer from this Spark Scala program for Input : s= ‘aaabbbccaabb’ Output : 3a3b2c2a2b

You can foldLeft over the input string, with a state of List[(Char, Int)]. Note that if you use Map[Char, Int], all occurrences of each character would be added up, weather they’re beside each other or not. s.foldLeft(List.empty[(Char, Int)]) { case (Nil, newChar) => (newChar, 1) :: Nil case ([email protected](headChar, headCount) :: tail, newChar) => if … Read more

[Solved] How can i make a for loop with if else and make the good return type

Your use of for does not behave as you expect. You’re using this for-comprehension: for (data <- category(i)) { if (data.startsWith(x)) true else false } This expression “desugars” into (i.e. is shorthand for): category(i).foreach(data => { if (data.startsWith(x)) true else false }) foreach returns Unit, and therefore the type of this expression (and the type … Read more

[Solved] Given the file path find the file extension using Scala?

You could achieve this as follows: import java.nio.file.Paths val path = “/home/gmc/exists.csv” val fileName = Paths.get(path).getFileName // Convert the path string to a Path object and get the “base name” from that path. val extension = fileName.toString.split(“\\.”).last // Split the “base name” on a . and take the last element – which is the extension. … Read more

[Solved] Input path does not exist

The error is pretty self explanatory, so it is probably something simple that you are missing. Can you modify your script and run it as shown below. Please modify the “fileName” value to where you think the file is. import java.nio.file.{Paths, Files} import sys.process._ /************ Modify this line with your data’s file name **************/ val … Read more

[Solved] Scala Filter Only Digits

I think this might solve your problem t.countByValue().filter(tupleOfCount=>Try(tupleOfCount._1.toInt).toOption.isEmpty).print() Use of isInstanceOf should be the last resort as @sergey said , so this code must solve the issue or else the pattern matching would be a good option too. solved Scala Filter Only Digits

[Solved] How can I access a method which return Option object?

The most iconic way ot do it is to unwrap values with scala is to use pattern matching to unwrap the value. entities match { case Some(queryEntities: QueryEntities) => queryEntities.entities.foreach { case e => println(e.columnFamily) println(e.fromDate.getOrElse(“defaultFromDateHere”) println(e.toDate.getOrElse(“defaultToDateHere”)) } case None => println(“No value”) } 9 solved How can I access a method which return Option … Read more