Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个字符串并突出显示发现的不匹配项_Java_String - Fatal编程技术网

Java 比较两个字符串并突出显示发现的不匹配项

Java 比较两个字符串并突出显示发现的不匹配项,java,string,Java,String,我想比较两个字符串,并在不匹配的地方突出显示单词 我编写的代码有以下两个问题: 一,。如果存在不匹配,则单词将存储在“token”中,但一旦调用highlight函数,它将突出显示该字符串中与token中的单词匹配的所有单词 二,。所有差异都显示在控制台上。但是,在加载GUI时,仅突出显示数组索引中的最后一个值,因此不会显示以前的突出显示 这是到目前为止的代码。我还附上了快照以供参考。任何建议都会大有帮助。谢谢 package com.check; import java.lang.refle

我想比较两个字符串,并在不匹配的地方突出显示单词

我编写的代码有以下两个问题:

一,。如果存在不匹配,则单词将存储在“token”中,但一旦调用highlight函数,它将突出显示该字符串中与token中的单词匹配的所有单词

二,。所有差异都显示在控制台上。但是,在加载GUI时,仅突出显示数组索引中的最后一个值,因此不会显示以前的突出显示

这是到目前为止的代码。我还附上了快照以供参考。任何建议都会大有帮助。谢谢

package com.check;

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class highlightCheck {

int i, j, x;

int Flag;
  int length;
 //int errorIndex;

int errorIndex[] = new int[2];
int loop;

String s1 = "";
String s2 = "";

String Retext1 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases on short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";
String Retext2 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases in short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";


String token1;
String token2;

public static void main(String args[]) {

    try {

        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                new highlightCheck().createAndShowGUI();
            }
        });

    } catch (InterruptedException ex) {
        ex.printStackTrace();

    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
    }
}

public void createAndShowGUI() {

    JFrame frame = new JFrame("Error-Highlighter");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    JTextField field = new JTextField();
    field.setBounds(60,  100,  60,  80);
    field.setSize(new Dimension(500,200));
    field.setBackground(Color.LIGHT_GRAY);
    field.setVisible(true);                 


    field.setEditable(false);


    frame.add(field);

    JTextArea area = new JTextArea(30, 50);
    area.setLineWrap(true);
    area.setWrapStyleWord(true);
    area.setEditable(false);


    for(i = 0; i < Retext1.length(); i++) {         

        char splitArray1 [] = Retext1.toCharArray();  

        s1 = s1 + splitArray1[i];           
    }


    System.out.println(s1.charAt(s1.length() - 1));
    area.setText(s1);


    for(j = 0; j < Retext2.length(); j++) {         

        char splitArray2 [] = Retext2.toCharArray();  

        s2 = s2 + splitArray2[j];           
    }

    System.out.println(s2.charAt(s2.length() - 1));
    field.setText(s2);




    String SplitArray1[] = Retext1.split(" ");
    String SplitArray2[] = Retext2.split(" ");

    //System.out.println(SplitArray1.length);

  if(SplitArray1.length > SplitArray2.length)  
      length = SplitArray1.length;
  else if(SplitArray2.length > SplitArray1.length)
      length = SplitArray2.length;
  else length = SplitArray2.length;


          for(x = 0; x < length; x++) { 

        if(SplitArray1[x] == SplitArray2[x]) 
            continue;

        else if(!SplitArray1[x] .equals(SplitArray2[x])) {



                token1 = "" + SplitArray1[x];
                token2 = "" + SplitArray2[x];


                System.out.print(x);
                System.out.print("  :  ");
                System.out.print(token1);
                System.out.print("  :  ");
                System.out.print(token2);
                System.out.println();

                highlight(area, token1);                             
                highlight(field, token2);                       

            }   

       }      


    frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);               
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}


public void highlight(JTextComponent textComp, String pattern) {

    removeHighlights(textComp);

    try {

        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());

        int position = 0;

        while ((position = text.indexOf(pattern, position)) >= 0) {

            hilite.addHighlight(position, position + pattern.length(), myHighlightPainter);
            position += pattern.length();
        }

    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}


public void removeHighlights(JTextComponent textComp) {


    Highlighter hilite = textComp.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

     for (int i = 0; i < hilites.length; i++) {

         if (hilites[i].getPainter() instanceof MyHighlightPainter) {
            hilite.removeHighlight(hilites[i]);
        }
    }
}

Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);


class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

    public MyHighlightPainter(Color color) {

        super(color);

    }
  }
}
package.com.check;
导入java.lang.reflect.InvocationTargetException;
导入javax.swing.*;
导入javax.swing.text.*;
导入java.awt.*;
公共类高亮检查{
int i,j,x;
int标志;
整数长度;
//国际错误指数;
int errorIndex[]=新的int[2];
内环;
字符串s1=“”;
字符串s2=“”;
字符串Retext1=“极限编程是敏捷软件开发的一种方法,它强调在称为时间盒的短开发周期内频繁发布。这通过具有多个短开发周期而不是一个长开发周期来减少变更成本。极限编程包括成对编程(用于代码审查、单元测试)。此外,它还避免实现当前时间框中未包含的功能,因此可以将进度缓慢降至最低。“;
字符串Retext2=”极限编程是敏捷软件开发的一种方法,它强调在称为时间盒的短开发周期内频繁发布。这通过具有多个短开发周期而不是一个长开发周期来减少变更成本。极限编程包括成对编程(用于代码审查、单元测试)。此外,它还避免实现当前时间框中未包含的功能,因此可以将进度缓慢降至最低。“;
字符串标记1;
字符串标记2;
公共静态void main(字符串参数[]){
试一试{
SwingUtilities.invokeAndWait(新的Runnable(){
公开募捐{
新建highlightCheck().createAndShowGUI();
}
});
}捕获(中断异常例外){
例如printStackTrace();
}捕获(调用TargetException ex){
例如printStackTrace();
}
}
public void createAndShowGUI(){
JFrame frame=新JFrame(“错误高亮显示”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField=新的JTextField();
字段.立根(60,100,60,80);
字段设置大小(新尺寸(500200));
退根地(颜色:浅灰色);
字段设置为可见(true);
字段设置为可编辑(false);
帧。添加(字段);
JTextArea=新的JTextArea(30,50);
区域。setLineWrap(真);
area.setWrapStyleWord(true);
区域设置可编辑(假);
对于(i=0;iSplitArray2.length)
长度=拆分阵列1.长度;
else if(SplitArray2.length>SplitArray1.length)
长度=SplitArray2.length;
else length=SplitArray2.length;
对于(x=0;x=0){
hilite.addHighlight(position,position+pattern.length(),myHighlightPainter);
位置+=模式长度();
}
}捕获(错误位置异常e){
e、 printStackTrace();
}
}
公共void removeHighlights(JTextComponent textComp){
Hilliter hilite=textComp.getHighlighter();
hilliter.Highlight[]hilites=hilite.getHighlights();
for(int i=0;i
而不是尝试在
文档中查找可能多次出现的标记(因为您没有提供上下文)
int max = Math.min(doc1.getLength(), doc2.getLength());
int startPos = 0;
try {
    for (int pos = 0; pos < max; pos++) {

        if (doc1.getText(pos, 1).equals(" ")) {

            int endPos = pos;
            String parent = doc1.getText(startPos, endPos - startPos);
            String child = doc2.getText(startPos, endPos - startPos);
            if (!parent.equals(child)) {

                highlight(field, startPos, endPos);
                highlight(area, startPos, endPos);

            }

            startPos = endPos + 1;

        }

    }
} catch (BadLocationException exp) {
    exp.printStackTrace();
}
import java.awt.Color;
import java.awt.GridLayout;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

public class HighlightCheck {

    private String Retext1 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases on short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";
    private String Retext2 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases in short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";

    private Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

    public static void main(String args[]) {

        try {

            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    new HighlightCheck().createAndShowGUI();
                }
            });

        } catch (InterruptedException ex) {
            ex.printStackTrace();

        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    public void createAndShowGUI() {

        JFrame frame = new JFrame("Error-Highlighter");
        frame.setLayout(new GridLayout(2, 1));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea field = new JTextArea(5, 50);
        field.setText(Retext2);
        field.setLineWrap(true);
        field.setWrapStyleWord(true);
        field.setEditable(false);

        frame.add(new JScrollPane(field));

        JTextArea area = new JTextArea(5, 50);
        area.setText(Retext1);
        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        area.setEditable(false);

        Document doc1 = field.getDocument();
        Document doc2 = area.getDocument();

        int max = Math.min(doc1.getLength(), doc2.getLength());
        int startPos = 0;
        try {
            for (int pos = 0; pos < max; pos++) {

                if (doc1.getText(pos, 1).equals(" ")) {

                    int endPos = pos;
                    String parent = doc1.getText(startPos, endPos - startPos);
                    String child = doc2.getText(startPos, endPos - startPos);
                    if (!parent.equals(child)) {

                        highlight(field, startPos, endPos);
                        highlight(area, startPos, endPos);

                    }

                    startPos = endPos + 1;

                }

            }
        } catch (BadLocationException exp) {
            exp.printStackTrace();
        }

        frame.getContentPane().add(new JScrollPane(area));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void highlight(JTextComponent textComp, int startPos, int endPos) throws BadLocationException {

        Highlighter hilite = textComp.getHighlighter();
        hilite.addHighlight(startPos, endPos, myHighlightPainter);

    }

    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

        public MyHighlightPainter(Color color) {

            super(color);

        }
    }
}