1

I am very new to scala.

Wrote a very basic scala script as below.

args.foreach(a=>println(a))

Executed the above in command prompt using the following

scala test1.scala Happy Scala Programming !!!

The output that I expected is

Happy
Scala
Programming
!!!

But I got the following instead

Happy
Scala
Programming

As we observe the "!!!" (exclamation characters are missing).

Need to understand why exclamation characters are not printed and what is way to get those characters printed.

I am executing the script on Windows 7 command prompt

TechEnthusiast
  • 1,795
  • 2
  • 17
  • 32
  • 3
    It could have something to do with the shell you are using consuming those characters and doing something with them before executing the command and passing the arguments to your program. You might want to mention what your run environment is, and most specifically the command shell you are using. – Loduwijk Mar 16 '17 at 16:10
  • What happens if you wrap the `!!!` in double-quotes? – Kevin Meredith Mar 16 '17 at 16:52
  • c:\scala\samples>scala test1.scala Happy Scala Programming "!!!" Happy Scala Programming – TechEnthusiast Mar 16 '17 at 16:59

1 Answers1

5

You have to use ^ before each exclamation signs and also need to put the whole argument in double quote. So this will work:

scala test1.scala "Happy Scala Programming ^!^!^!"

This is because ! is a special character for the windows shell. So this issue has nothing to do with the scala itself as scala program is receiving only Happy Scala Programming as program arguments, the exclamations were getting consumed by the windows shell prompt.

Please take a look at these stack overflow answers for more in depth explanations.

  1. https://stackoverflow.com/a/28892829/4046067
  2. https://stackoverflow.com/a/15322106/4046067
Community
  • 1
  • 1
Tawkir
  • 1,176
  • 7
  • 13
  • 1
    Since my requirement was to print each word in a new line hence I used the following (added double quotes only around the special characters). Many thanks for the help. scala test1.scala Happy Scala Programming "^!^!^!" – TechEnthusiast Mar 17 '17 at 11:23