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 6195755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:31:24+00:00 2026-05-24T03:31:24+00:00

Yesterday, this piece of code caused me a headache. I fixed it by reading

  • 0

Yesterday, this piece of code caused me a headache. I fixed it by reading the file line by line. Any ideas ?

The while loop never seems to get executed even though the no of lines in the file is greater than 1.

 val lines = Source.fromFile( new File("file.txt") ).getLines;

 println( "total lines:"+lines.size );

 var starti = 1;
 while( starti < lines.size ){
   val nexti = Math.min( starti + 10, lines.size  );

   println( "batch ("+starti+", "+nexti+") total:" + lines.size )
   val linesSub = lines.slice(starti, nexti)
   //do something with linesSub
   starti = nexti
 }
  • 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-24T03:31:25+00:00Added an answer on May 24, 2026 at 3:31 am

    This is indeed tricky, and I would even say it’s a bug in Iterator. getLines returns an Iterator which proceeds lazily. So what seems to happen is that if you ask for lines.size the iterator goes through the whole file to count the lines. Afterwards, it’s “exhausted”:

    scala> val lines = io.Source.fromFile(new java.io.File("....txt")).getLines
    lines: Iterator[String] = non-empty iterator
    
    scala> lines.size
    res4: Int = 15
    
    scala> lines.size
    res5: Int = 0
    
    scala> lines.hasNext
    res6: Boolean = false
    

    You see, when you execute size twice, the result is zero.

    There are two solutions, either you force the iterator into something ‘stable’, like lines.toSeq. Or you forget about size and do the “normal” iteration:

    while(lines.hasNext) {
      val linesSub = lines.take(10)
      println("batch:" + linesSub.size)
      // do something with linesSub
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I created this piece of code that could calculate z^n, where z is
I saw some code yesterday in this question that I had not seen before,
Yesterday I wrote a piece of code to remove all the controls in a
the following piece of code worked fine all day yesterday public Image getImage() {
I could swear this was working yesterday. Now however the code below destroys the
Some of what I've been reading about this yesterday and today: (SO) Should you
I worked on this yesterday about 5 hours and got the code to work
Note: I asked this yesterday over at kiln.stackexchange.com , but haven't gotten an answer,
My fact table looks like this yesterday a yesterday a yesterday a yesterday b
A friend asked me yesterday if this was possible on the iPhone. I took

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.