Write a code to print a uniformly generated list with input, number of Strings of Scala, and input number of elements such that the n-th element is n+1

Hi All,

AM new to this scala. this is one of the lab assignment . am trying to find the solution. and failed many time. finally decided to get some help to finish this task.
Not sure where am making mistake.
Kindly help me to sort it out.

Expected output.
Output will be List(‘Scala’, ‘Scala’, ‘Scala’) List(1,2,3)

This is the code am using to get the output.

object ListFunctions2{
def main(args :Array[String]){
val input :Int = args(0).toInt
val uniformList = List.fill(3)(“Scala”)
val tabulateList =List.tabulate(input3)(n=>n+1)
println(uniformList,tabulateList)
}
}

Below is the output that am getting
(List(Scala,Scala,Scala),List(1,2,3))

First, for following posts, you can format your code like this:

```scala
// Your code here.
```

And it will look like this:

object ListFunctions2 {
  def main(args: Array[String]): Unit = {
    val input: Int = args(0).toInt
    val uniformList = List.fill(3)(“Scala”)
    val tabulateList = List.tabulate(input3)(n => n + 1)
    println(uniformList,tabulateList)
  }
}

Much better, isn’t it?
(BTW, I made another style adjustments, check them out)

Now, returning to your problem. It seems the only problem is the way you print the results.
Try with this:

println(s"${uniformList} ${tabulateList}")

BTW, I am pretty sure that you also wanted to use input instead of input3 and 3, when creating the lists.

Hi San,

Thanks for the code. it ot was give the extact output. without quoates.

scala ListFunctions2.scala 3
List(Scala, Scala, Scala)List(1, 2, 3)

i tried using escape characters but no luck

So if you want the quotes in the output, I believe the simplest solution would be to put them during the fill.

val uniformList = List.fill(3)("'Scala'") // Note the single quotes after the double quotes.

Or if you need / want the output to have double quotes, you can do this:

val uniformList = List.fill(3)("\"Scala\"") // Note the scaped quotes.

HI BalmungSan,

Much Appreciated i have used the Escape character. BUt still it shows that am not completed the task

object ListFunctions2{
def main(args :Array[String]){
val input :Int = args(0).toInt
val uniformList = List.fill(3)("‘Scala’")
val tabulateList =List.tabulate(3)(n=>n+1)
println(s"${uniformList}${tabulateList}")
}
}

Actual Code Output
$ scala ListFunctions2.scala 3
List(‘Scala’, ‘Scala’, ‘Scala’)List(1, 2, 3)
$

is it because my list contains sapace here (List(1, 2, 3) ?

Expected Output
Output will be List(‘Scala’, ‘Scala’, ‘Scala’) List(1,2,3)

So it seems you are either missing an space between both lists.
But, I would say that maybe you should print each one in their own line.

Try this, it worked for me

object ListFunctions2{
def main(args :Array[String]){
val input :Int = args(0).toInt
val uniformList = List.fill(input)(“Scala”)
val tabulateList =List.tabulate(input)(n=>n+1)
println(uniformList,tabulateList)
}
}