Linda Su
Linda Su

Reputation: 455

How to format string to right and left justify while writing to file in scala?

I'm trying to write two strings to a file:

var str1 = "this is" var str2 = "a good day"

such that str1 should be written right justified and str2 left justified.

I read that it works something like this:

fw.write("%" + str1)
fw.write("%-" + str2)

But, it's not giving me the expected result. Why is that?

EDIT:

val fw = new FileWriter("results.txt", true) ;

Upvotes: 0

Views: 1901

Answers (1)

suztomo
suztomo

Reputation: 5202

"+" only concatenates two strings. Try format method.

fw.write("%-10s".format(str2))

Upvotes: 1

Related Questions