0

I am trying to create multiple files. For example, t1.txt, t2.txt, etc. I was thinking of something like code below, but it doesn't work:

 val nbFiles = 15
 for(n <- 1 to nbFiles) {
     val writer = new PrintWriter(new File("t$n.txt"))
     writer.write("Hello Scala")
     writer.close()
 }

I need a for loop for making some process on each file.

1 Answer 1

2

You need to interpolate the string by preceding it with an s:

val writer = new PrintWriter(new File(s"t$n.txt"))
                                      ^
Sign up to request clarification or add additional context in comments.

Comments

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.