Home > Net >  How can we compare two Strings and get duplicates values
How can we compare two Strings and get duplicates values

Time:02-24

I created a window with a button and 3 fields, 1st, 2nd field is the text, and in the 3rd field, it is necessary to write down words that are repeated in 1st & 2nd fields. How to make it?

        JFrame frame = new JFrame("new Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        Label lbTextLabel = new Label("1:");
        lbTextLabel.setBounds(50, 10, 120, 20);
 
        Label lbTextLabel2 = new Label("2:");
        lbTextLabel2.setBounds(50, 50, 120, 20);
 
        Label lbTextLabel3 = new Label("3:");
        lbTextLabel3.setBounds(50, 90, 120, 20);
 
        JTextArea TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,20);
 
        JTextArea TextArea2 = new JTextArea("Welcome to javatpoint 2");
        TextArea2.setBounds(50,70, 127,20);
 
        JTextArea TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,110, 127,20);
 
        JButton button =new JButton("Click Here");
        button.setBounds(140,130,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
            }
        });

CodePudding user response:

You have to split both textarea word and check if first textarea word is matched with second textarea word, If match print it in third textarea.

Here down is example:

import java.awt.event.*;  
import javax.swing.*;    
import java.util.*;
class CompareWord{
    public static void main(String[] args) {
        JFrame f;
        JLabel lbTextLabelt1, lbTextLabel2, lbTextLabel3;
        JTextArea TextArea, TextArea2, TextArea3;
        JButton button;

        f = new JFrame("new Frame");
        lbTextLabelt1 = new JLabel("1:");
        lbTextLabelt1.setBounds(50, 10, 120, 20);

        lbTextLabel2 = new JLabel("2:");
        lbTextLabel2.setBounds(50, 70, 120, 20);

        lbTextLabel3 = new JLabel("3:");
        lbTextLabel3.setBounds(50, 140, 120, 20);

        TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,30);

        TextArea2 = new JTextArea("Welcome to javatpoint");
        TextArea2.setBounds(50,90, 127,30);

        TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,160, 127,30);

        button = new JButton("Click Here");
        button.setBounds(120,200,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                String str1 = TextArea.getText();  //            
  • Related