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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:51:01+00:00 2026-06-10T21:51:01+00:00

Need some help to find the number of matched tokens between two strings. I

  • 0

Need some help to find the number of matched tokens between two strings. I have a list of string stored in ArrayList (example given below):

Line 0 : WRB VBD NN VB IN CC RB VBP NNP  
Line 1 : WDT NNS VBD DT NN NNP NNP  
Line 2 : WRB MD PRP VB DT NN IN NNS POS JJ NNS  
Line 3 : WDT NN VBZ DT NN IN DT JJ NN IN DT NNP  
Line 4 : WP VBZ DT JJ NN IN  NN  

Here, you can see each string consists of a bunch of tokens separated by spaces. So, there’s three things I need to work with..

  1. Compare the first token (WRB) in Line 0 to the tokens in Line 1 to see if they match. Move on to the next tokens in Line 0 until a match is found. If there’s a match, mark the matched tokens in Line 1 so that it will not be matched again.
  2. Return the number of matched tokens between Line 0 and Line 1.
  3. Return the distance of the matched tokens. Example: token NN is found on position 3 on line 0 and position 5 on Line 1. Distance = |3-5| = 2

I’ve tried using split string and store it to String[] but String[] is fixed and doesn’t allow shrinking or adding of new elements. Tried Pattern Matcher but with disasterous results. Tried a few other methods but there’s some problems with my nested for loops..(will post part of my coding if it will help).

Any advice or pointers on how to solve this problem this would be very much appreciated. Thank you very much.

  • 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-10T21:51:02+00:00Added an answer on June 10, 2026 at 9:51 pm

    Have you tried using Scanner?

    If not, totally do. It would look like this:

    String line1 = ... // your line 1
    String line2 = ... // your line 2
    Scanner s1 = new Scanner(line1); 
    
    int i1 = 0;
    while (s1.hasNext()) {
        String token1 = s1.next();
        Scanner s2 = new Scanner(line2);
    
        int i2 = 0;
        while (s2.hasNext()) {
            String token2 = s2.next();
    
            // now you have token1, token2 and their positions (i1, i2)
            // do whatever you want with them
    
            i2++;
        } // end reading line2
        i1++;
    } // end reading line1
    

    EDIT: Regarding your loops to select different lines in the Arraylist, what you need is to compare every array element to every other array element (which is probably the best thing to google if this explanation is lacking).

    In Java that looks like this:

    for (int i = 0; i < thearraylist.size()-1; i++) {
        for (int j = i+1; j < thearraylist.size(); j++) {
    
            // now the elements and indices i and j are compared
            // if we were running into my code above:
    
            String line1 = thearraylist.get(i);
            String line2 = thearraylist.get(j);
    
            // ... and then compare them
    
         }
    }
    

    The reason the second loop starts from i+1 is to eliminate these unnecessary comparisons:

    1. Every element would be compared to itself at each point that j=i, which is useless. In the above loop, j starts at i+1 and increases, so it will never equal i.
    2. Each pair of elements will be compared twice. For example, when i=0, j=1 you are comparing the first two elements. When i=1, j=0 you are also comparing the first two elements. This makes the second comparison redundant. To get rid of the second ‘backwards’ comparison, we insist that j always be higher than i.

    If you find this confusing, I would highly recommend working it out on paper by listing the values of i and j as you move through the loop.

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

Sidebar

Related Questions

I need some help with data analysis. I do have two datasets (before &
I need some help sending a file over a serial connection. I have two
So I need some help coming up with a way to find a Minimum
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help with a query.. I have three tables. Source id name 1
I need some help, I have to make a project about leaves. I want
I have a problem I would like some help at. I need to create

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.