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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:42:34+00:00 2026-05-26T10:42:34+00:00

I have the following text fil, I’m wondering how can I parse it and

  • 0

I have the following text fil, I’m wondering how can I parse it and search for Cell And Engine words, what I want is to print out the method name that contains Cell, Engine words. the following is the txt file don’t look at it as java code since I already moved it to txt file for parsing purposes.

@Test
  public void testGetMonsters() {
        Cell cell11 = aBoard.getCell(1, 1);
        theEngine = new Engine(theGame);   
  }

  @Test
  public void testDxDyPossibleMove() {
        Cell cell11 = aBoard.getCell(1, 1);
  }   

The desired output of parsing looks like:

testGetMonsters class contains Cell and Engine words
testDxDyPossibleMove class contains Cell word
  • 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-26T10:42:35+00:00Added an answer on May 26, 2026 at 10:42 am

    Tadaaaa!

    package textsearch;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.net.URL;
    import java.text.ParsePosition;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class TextSearch {
    
        public static void main(final String[] args) throws Exception {
    
            final URL testFileURL = Thread.currentThread().getContextClassLoader().getResource("textsearch/test.txt");
            final File input = new File(testFileURL.toURI());
            final FileReader reader = new FileReader(input);
    
            final SearchString search1 = new SearchString("Cell");
            final SearchString search2 = new SearchString("Engine");
    
            final List<SearchString> searches = new ArrayList<SearchString>();
            searches.add(search1);
            searches.add(search2);
    
            final Search search = new Search(searches);
    
            try {
                search.search(reader);
            } finally {
                reader.close();
            }
    
            if(search.hasPositives()) {
                System.out.print("File " + input.getName() + " contains the words ");
                search.printPositives();
                System.out.println("");
            } else {
                System.out.println("File " + input.getName() + " didn't contain any of the search terms.");
            }
    
        }
    
        private static class SearchString {
    
            final String searchString;
            final ParsePosition pos;
    
            SearchString(final String searchString) {
    
                if(searchString == null || searchString.isEmpty())
                    throw new IllegalArgumentException("I can't search for nothing!");
    
                this.searchString = searchString;
                pos = new ParsePosition(0);
    
            }
    
            boolean checkNextChar(final char c) {
    
                if(searchString.charAt(pos.getIndex()) == c) {
                    pos.setIndex(pos.getIndex() + 1);
                    if(pos.getIndex() >= searchString.length()) {
                        pos.setIndex(0);
                        return true;
                    }
                    return false;
                } else {
                    pos.setIndex(0);
                    return false;
                }
    
            }
    
            String getString() {
    
                return searchString;
    
            }
    
        }
    
        private static class Search {
    
            private final List<SearchString> searches;
            private final List<SearchString> positives;
    
            Search(final List<SearchString> searches) {
    
                this.searches = searches;
                positives = new ArrayList<SearchString>();
    
            }
    
            void search(final Reader reader) throws IOException {
    
                int current;
    
                while((current = reader.read()) != -1 && !searches.isEmpty()) {
    
                    char c = (char)current;
    
                    for(final Iterator<SearchString> it = searches.iterator(); it.hasNext();) {
                        final SearchString searchString = it.next();
                        final boolean matches = searchString.checkNextChar(c);
                        if(matches) {
                            positives.add(searchString);
                            it.remove();
                        }
                    }
    
                }
    
            }
    
            boolean hasPositives() {
    
                return !positives.isEmpty();
    
            }
    
            void printPositives() {
    
                for(final Iterator<SearchString> it = positives.iterator(); it.hasNext();) {
                    final SearchString searchString = it.next();
                    System.out.print(searchString.getString());
                    if(it.hasNext())
                        System.out.print(", ");
                }
    
            }
    
        }
    
    }
    

    Alright, it ain’t 100% reliable, but it’s a start.

    EDIT: or you can use a Scanner like KayKay suggested, but where’s the fun in that 😀

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

Sidebar

Related Questions

I have the following text that I want to strip out and grab the
I have the following text; country=france name=jean country=germany name=michael country=england name=jack I want it
I have the following text: txtAddressSup.Text I want the Sup to be replaced by
I have the following text: 3/1 I want to amend it to be: 3/1
I have the following text Car/red i want it to be sent withing the
I have the following text as an example: \n\t\t\t\t\t3 comments Can anyone help me
I have the following text that I am trying to parse: user1@emailaddy1.com <user1@emailaddy1.com>, Jane
I have the following text that I want to capture as a table: question,answer
I have the following text below, what is the easiest way to parse through
I have the following text: <!--:en-->&nbsp; <!--:--> I want to construct a pattern 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.