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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:28:17+00:00 2026-06-14T12:28:17+00:00

I am wondering how do we write Java code from the following PseudoCode foreach

  • 0

I am wondering how do we write Java code from the following PseudoCode

 foreach file F in file directory D
        foreach int I in file F
               sort all I from each file

Basically this is part of the External Sorting algorithm, so those files contain lists of sorted integer, and I want to read the first one from each file and sort it and then output to another file, and then move to the next integer from each file again until all the integers are fully sorted.
The problem is that as far as I understand for each file we need a reader, so if we have N files then does that mean we need N file readers?

======update=======

I am wondering is it something that look like this? Correct me if I miss anything or any other better approach.

int numOfFiles = 10;
Scanner [] scanners = new Scanner[numOfFiles];
try{
    //reader all the files
    for(int i = 0 ; i < numOfFiles; i++){
        scanners[i] = new Scanner(new BufferedReader(
            new FileReader("file"+i+".txt");
    }
}
catch(FileNotFoundException fnfe){

}
  • 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-06-14T12:28:18+00:00Added an answer on June 14, 2026 at 12:28 pm

    The problem is that as far as I understand for each file we need a reader, so if we have N files then does that mean we need N file readers ?

    Yes, that’s right – unless you want to either have to go back over the data, or the whole of each file into memory. Either of those would let you get away with only one file open at a time – but that may well not suit what you want to do.

    Operating systems usually only allow you to open a certain number of files at a time. If you’re trying to do something like create a single sorted set of results from a very large number of files, you might want to consider operating on a few of them at a time, producing larger intermediate files. At its simplest, this would just sort two files at a time, e.g.

    input1 + input2 => tmp-a1
    input3 + input4 => tmp-a2
    input5 + input6 => tmp-a3
    input7 + input8 => tmp-a4
    
    tmp-a1 + tmp-a2 => tmp-b1
    tmp-a3 + tmp-a4 => tmp-b2
    
    tmp-b1 + tmp-b2 => result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I write a Java program I always try to organize my code following
I'm wondering how can I write a function: 'a list*int -> 'a list*list that
I was wondering how to better write code like this so that it leverages
I am reverse engineering some Java code into a class diagram. Now I'm wondering
I am beginner to java and was trying out this code puzzle from the
I understand that Java can load/execute DLL code, but I'm wondering if there are
I've added another log file and I was wondering how I access it from
Coming from Java, I'm wondering if a Java best practice applies to JavaScript. In
I was wondering if it's possible to write PHP code inline within Spring MVC
I was wondering if it is in JAVA to write the calculated data to

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.