Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:02:00+00:00 2026-05-15T12:02:00+00:00

I’ve tried to run this code under scala 2.7.3 and 2.7.7 and everytime i

  • 0

I’ve tried to run this code under scala 2.7.3 and 2.7.7 and everytime i get this error. What is wrong?

import scala.io._

def toInt(in: String): Option[Int] =
  try {
    Some(Integer.parseInt(in.trim))
  } catch {
    case e: NumberFormatException => None
  }

def sum(in: Seq[String]) = {
  val ints = in.flatMap(s => toInt(s))
    ints.foldLeft(0)((a, b) => a + b)
  }

println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
val input = Source.fromInputStream(System.in)
val lines = input.getLines.collect
println("Sum "+sum(lines))

alt text

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:02:01+00:00Added an answer on May 15, 2026 at 12:02 pm

    I’ve tried to isolate the problem and it seems that it has to do with the way you input your values.

    For example, the following code snippet works as expected, printing 6.

    import scala.io._
    
    def toInt(in: String): Option[Int] =
      try {
        Some(Integer.parseInt(in.trim))
      } catch {
        case e: NumberFormatException => None
      }
    
    def sum(in: Seq[String]) = {
      val ints = in.flatMap(s => toInt(s))
      ints.foldLeft(0)((a, b) => a + b)
    }
    
    println(sum(List("1","2","3")))
    

    Therefore, the problem must lie somewhere in the lines to follow. I think the main culprit is what Ctrl-C actually does on Windows combined with how the Scala interpreter runs your scripts. The last line of the screenshot you have attached to the question was the starting point. It suggests that somewhere, a batch job is running and the Ctrl-C has sent a signal to kill that batch job. This is actually none other than the scala interpreter itself (scala.bat). Therefore, when you press Ctrl-C, instead of just killing the input stream, you send a signal to kill the whole batch job running your program!

    So the error has something to do with this action. I’ve looked at how scala runs your script. It seems that a folder named scalascriptXXXXXXXXXXXXXXXXXXX (where XX… represent 19 pseudorandom digits) is created in %TEMP% where all the compiled stuff needed is placed. There, a file called Main$$anon$1$$anonfun$1.class is created — this represents the anonymous function that is passed to the flatMap function. It seems that this file is created in parallel with the running of the program. Therefore, if you kill your process before it has a chance to do this compilation, you will get the ClassNotFoundException.

    Now, it’s quite strange that you get this error all the time you run your script. I have managed to get it occasionally. Always though, as soon as I press Ctrl-C I get the Terminate batch job (Y/N)? prompt. Sometimes it is accompanied with an exception. Sometimes not. I think it again has to do with the creation of the Main$$anon$1$$anonfun$1.class. If I wait a while, I get no exception. If I press Ctrl-C straight away after start, I get the exception.

    After quite a lot of runs I managed to get a complete stack trace(I think… as the Ctrl-C stops the stack trace from being dumped as well), pointing to the main culprit — a FileNotFoundException.

    scala sum.scala
    Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)
    1
    2
    3
    4
    java.lang.NoClassDefFoundError: Main$$anon$1$$anonfun$1
            at Main$$anon$1.sum((virtual file):15)
            at Main$$anon$1.<init>((virtual file):25)
            at Main$.main((virtual file):4)
            at Main.main((virtual file))
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
            at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
            at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
            at scala.tools.nsc.ScriptRunner$.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:381)
            at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:414)
            at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:413)
            at scala.tools.nsc.ScriptRunner$.withCompiledScript(ScriptRunner.scala:351)
            at scala.tools.nsc.ScriptRunner$.runScript(ScriptRunner.scala:413)
            at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:168)
            at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
    Caused by: java.lang.ClassNotFoundException: Main$$anon$1$$anonfun$1
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
            ... 18 more
    Caused by: java.io.FileNotFoundException: C:\Users\****\AppData\Local\Temp\scalascript5827128241389779296\Main$$anon$1$$anonfun$1.class (T
    he system cannot find the file specified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:106)
            at sun.misc.URLClassPath$FileLoader$1.getInputStream(URLClassPath.java:1001)
            at sun.misc.Resource.cachedInputStream(Resource.java:59)
            at sun.misc.Resource.getByteBuffer(Resource.java:154)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:249)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
            ... 23 more
    Terminate batch job (Y/N)? y
    

    I would therefore recommend using a better method to input your values. Maybe try Console.in.readLine? and stop when you get a non-numeric line.

    Or, if you really want to indicate the end of a stream, try using Ctrl-Z (Ctrl-C is the kill process signal).

    scala sum.scala
    Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)
    1
    3
    44
    5
    6
    ^Z
    Sum 59
    

    — Flaviu Cipcigan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.