0

I just started fighting my way up the learning curve with Scala. I generally do this sort of thing by implementing the first twenty or so Euler problems. Things took a surprising turn when I hit #4.

/*
    A palindromic number reads the same both ways. 
    The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

    Find the largest palindrome made from the product of two 3-digit numbers.
*/

object Four
{
    def main(args: Array[String]) 
    {
        println("start");
        println("end");
    }
}

Although it might not jump out at you, the x between 91 and 99 in the comment is actually a unicode x which is encoded as a D7. If I change that to an ASCII X the program compiles and runs as expected. If I leave it as is, the program appears to run, but there is no output.

Running from the command line shows

c:\projects\Euler>scalac -feature euler4.scala
error: IO error while decoding euler4.scala with UTF-8
Please try specifying another one using the -encoding option
one error found

A little bit of research on SO suggests specifying the encoding

c:\projects\Euler>scalac -encoding UTF-8 -feature euler4.scala
error: IO error while decoding euler4.scala with UTF-8
Please try specifying another one using the -encoding option
one error found

My config is windows 10 with "Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121)."

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
  • Have you tried looking at http://stackoverflow.com/questions/23224219/does-the-scala-compiler-work-with-utf-8-encoded-source-files ? I tried adding this character to a file (in a commentary, of course) and it didn't break anything. – Cyrille Corpet Apr 02 '17 at 21:24
  • Unable to reproduce the problem. (Scala 2.12.1) What version are you running? – jwvh Apr 02 '17 at 21:51
  • 1
    If you are going to tell a program which encoding to use to read your file, you should tell it the encoding that _you used to save the file._ It seems UTF-8 would be a great choice. – Tom Blodget Apr 02 '17 at 22:27
  • @jwvh Thank you. I have added that information to the questions. – EvilTeach Apr 03 '17 at 00:11
  • @tom. Yes. I had assumed that it was straight ASCII. The comment is a cut and paste from the euler site. https://projecteuler.net/problem=4. I am basically using msvc 2015 as the text editor, and have a button that runs scala at the command line and dumps the output into the msvc output window. – EvilTeach Apr 03 '17 at 00:13
  • @Cyrille. Yes. That like was the source of the suggestion to specify the encoding as utf8. – EvilTeach Apr 03 '17 at 00:17
  • 1
    Visual Studio picks archaic encodings as the default for some types of files. To fix, File » Save As and drop down the options on the Save button or File » Advanced Save Options. – Tom Blodget Apr 03 '17 at 03:49

0 Answers0