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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:52:27+00:00 2026-06-15T01:52:27+00:00

I have a section of code like this: while (scanFile.hasNextLine()) { String currentLine =

  • 0

I have a section of code like this:

            while (scanFile.hasNextLine()) 
            {
                String currentLine = scanFile.nextLine();
                if (currentLine.isEmpty())
                {
                    System.out.println();
                    continue;
                }
                String[] allWordsInCurrentLine = currentLine.split(" ");    

I then have three separate methods I want to use to manipulate the file being scanned.

The first method scans each line of the code individually and prints out some form of output (pigLatin) line by line (so one line of output is given for each iteration of the “while” loop.
The second method scan all the text first and stores various information in different arrays and variables. The user can then search this information (so the entire “while” loop must be satisfied before any output can be given).
I then have a third method which does something similar to the first one.

I am trying to find the most efficient way to reuse this section of code. Originally, I tried just doing method calls underneath the bottom line of the code above and have a for loop at the top of it, so if a==0, call first method, then second time through while loop, a will be incremented and second method will be called and so on, like this:

for(String currentWordInCurrentLine : allWordsInCurrentLine)
{
    if (i==0)
        pigLatin(currentWordInCurrentLine);         // Part 1
    if (i==1)
        searchForWord(currentWordInCurrentLine, currentLine);   // Part 2
    if (i==2)
        brailleTranslator();
}

There are a few issues with this, not the least the fact it looks awful. Someone advised me to try interfaces, but they are a bit above my station (am only a few weeks into my course). Does anyone have any other suggestions?

Edit: The pigLatin method call works fine using the above (albeit horrible looking) way. However, I can’t call the next method like that, as it needs to run through the whole ‘while’ loop before it can operate.

  • 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-15T01:52:28+00:00Added an answer on June 15, 2026 at 1:52 am

    I would wrap the functionality you want with your three different processes in their own custom callable-like class, and then pass them to the code above. In this case the shared code would look something like this:

    public class FileHandlerManager {
    
        public static void processFile(File file, List<FileHandler> handlers) {
    
            // ... Setup your scanFile from your file... leaving this out.
    
            while (scanFile.hasNextLine()) {
    
                String currentLine = scanFile.nextLine();
    
                String[] allWordsInCurrentLine = currentLine.split(" ");
    
                for (FileHandler handler : handlers) {
                    handler.handle(allWordsInCurrentLine);
                }
            }
        }
    }
    

    Then you can share the functionality of that while loop across FileHandler. What is a FileHandler? I would define it very simply, like this:

    public interface FileHandler {
        public void handle(String[] allWordsInCurrentLine);
    }
    

    Your first one might look like this:

    public class PigLatinFileHandler implements FileHandler {
        public void handle(String[] allWordsInCurrentLine) {
            // Do your piglatin code.
        }
    }
    

    And finally, to run it, you would do something like this

    public static void main(String[] args) {
    
        IndexingFileHandler indexHandler = new IndexingFileHandler();
    
        List<FileHandler> handlers = new LinkedList<FileHandler>();
        handlers.add(new PigLatinFileHandler());
        handlers.add(indexHandler);
        handlers.add(new SimilarToPigLatinFileHandler());
    
        FileHandlerManager.processFile(file, handlers);
    
        // Note now you have a reference to indexHandler; so if you wanted to save
        // any state while processing the words you can, and use it here.
    }
    

    EDIT

    I just want to point out that there is an “easier”, less object-oriented way; you could have a method take a file and return a String[][], which is a two-dimensional array of file lines by words in those lines. Then you can pass around that array to your handlers.

    However the downside with this approach is that it breaks on large files (you have to load the whole thing into memory at once) and you don’t get any output until the entire file has been read. A (maybe non-obvious) advantage of my answer is that you “stream” the file; you process a line with all of the handlers, and then throw out the line, allowing java to reuse that memory. This code would let you handle a 10 GB file while only using java memory equal to the length of the longest line in that file (plus any state that your IndexHandler is saving).

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

Sidebar

Related Questions

I have this section of code: int numberStars =0; int counter =0; while (file.hasNext())
I have this view (DetailsContainer, first class in code section of this question) with
I have html output in paging section like this; <p>&nbsp;<strong class=active>1</strong>&nbsp;<a href=http://localhost/project/report_nc/search_now/1>2</a>&nbsp;<a href=http://localhost/project/report_nc/search_now/2>3</a>&nbsp;<a href=http://localhost/project/report_nc/search_now/1>Next
I have been trying to figure this little section of my larger project out
I have a section of code which should be executed by a maximum number
I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
I have some VERY simple code to return the title for a section header:
In my xaml code, inside the Window.Resources section, I have defined a Data Template
I have problem running and debugging this piece of code: bool readSectionHeaders(char* path, int

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.