Java:如何让扫描仪只匹配第一次出现,如果之前匹配过则跳过 [英] Java: How to get Scanner to match the first occurence only and skip if it has been matched before

查看:20
本文介绍了Java:如何让扫描仪只匹配第一次出现,如果之前匹配过则跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表,由存储在 ArrayList 中的空格分隔的标记组成.我需要扫描字符串 1 中的令牌是否存在于字符串 2 中.我设法使用 Scanner 来扫描令牌.但是,扫描仪没有做我想要的.

I have a list of strings consisting of tokens separated by spaces stored in an ArrayList. I need to scan if the tokens in string 1 are present in string 2. I managed to use Scanner to scan for the tokens. However, the Scanner doesn't do what I wanted.

我需要建议/建议
1. [已编辑] 示例:字符串 1 中有一个 NN 标记,但字符串中有两个 NN 标记2. 因此,扫描器应该扫描字符串 2 以获取 NN 令牌.使用我提供的代码,扫描器将搜索所有 NN 令牌,包括第二个 NN 令牌.它应该在第一个 NN 令牌处停止,而不是继续扫描所有 NN 令牌.{海莉关于休息的建议确实停止了扫描仪}
2. [已编辑] 现在,另一个问题是 - 如果字符串 1 有两个 NN 标记,则扫描器应该足够智能以跳过在先前扫描中找到的所有标记.扫描器应该能够将字符串 1 中的第二个 NN 标记与字符串 2 中的第二个 NN 标记匹配.

I need suggestions/advice on
1. Example: there's one NN token in String 1 but there's two NN tokens in String 2. So, the Scanner should scan String 2 for NN token. With the code I gave, the Scanner will search for all NN tokens, including the second NN token. It should stop at the first NN token and not continue scanning for all NN tokens. {Haley's advice on break does stop the Scanner}
2. Now, the other problem is - if String 1 has two NN tokens, the Scanner should be smart enough to skip all the tokens found in previous scans. The Scanner should be able to match the second NN token in String 1 to the second NN token in String 2.

这是我目前所拥有的..

This is what I have so far..

import java.util.ArrayList;
import java.util.Scanner;

public class TokenMatching {

    public static void main(String[] args) 
    {   
        ArrayList<String> taggedArray = new ArrayList<String>(); 

        //Example how the string would look like
        String string1 = "WRB VBD NN VB IN CC RB VBP NNP";
        String string2 = "WRB NN MD PRP VB DT NN IN NNS POS JJ NNS"; 

        taggedArray.add(string1);
        taggedArray.add(string2);       

        //Nested for loop to match taggedArray(i) with taggedArray(j)
        for(int i = 0; i< taggedArray.size(); i++)
        {
            for(int j = i + 1; j < taggedArray.size(); j++)
            {
                Scanner scan1 = new Scanner(taggedArray.get(i));

                int index1 = 0;
                while(scan1.hasNext())
                {
                    String token1;
                    token1 = scan1.next();
                    System.out.println(token1);
                    Scanner scan2 = new Scanner(taggedArray.get(j));

                    int index2 =0;
                    while(scan2.hasNext())
                    {
                        String token2 = scan2.next();

                        if(token1.equals(token2))
                        {
                            int relPosition;                            
                            relPosition = Math.abs(index1-index2);

                            //The print lines help me keep track of what is going on in the loop
                            System.out.println("Match found.");
                            System.out.println("Relative position for " + token1 + " : " + relPosition);

                        }
                            else
                            {
                                System.out.println("No Match Found.");
                            }

                        index2++;
                    }

                    index1++;
                }               
            }           
        }    
    }
}

任何建议都会有很大帮助.谢谢.

Any advice would be a great help. Thank you.

推荐答案

  1. 有没有办法让扫描器只扫描第一次出现并移动到字符串 1 中的下一个标记

在你的 if(token1.equals(token2)) 之后添加 break; System.out.println("Relative position for " + token1 +" : " + relPosition);.http://ideone.com/2o5Yz 生成的输出是您想要的吗?

In your if(token1.equals(token2)) add a break; after System.out.println("Relative position for " + token1 + " : " + relPosition);. Is the output generated by http://ideone.com/2o5Yz what you want?

这篇关于Java:如何让扫描仪只匹配第一次出现,如果之前匹配过则跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆