有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA从文本文件中找到几个单词或短语并显示出来

当我反向/反向输入词组时,我在查找词组时遇到了一些问题。例如我在找约翰·洛夫洛克的名字和姓氏。程序找到短语并显示给我。我找不到那根火柴,但我找到了。我怎样才能获得这种功能

以下是我得到的:

public class Programa extends JFrame implements ActionListener {
    private JTextArea text;

    public static void main(String[] args) {
        new Programa().setVisible(true);
    }

    public Programa() {
        super("Redagavimas");
        setSize(600, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        initialize(); 

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {


                JFrame frame = new JFrame("Paieska sarase");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Layout());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);


            }
        });
    }

    public void initialize(){

        text = new JTextArea();
        JScrollPane scroll = new JScrollPane(text);

        JMenuBar bar = new JMenuBar();
        JMenu file = new JMenu("Failas");
        JMenuItem open = new JMenuItem("Atidaryti");
        JMenuItem save = new JMenuItem("Issaugoti");
        JMenuItem exit = new JMenuItem("Uzdaryti");

        JMenuItem[] items = { open, save, exit};
        for(JMenuItem item : items) {
            item.addActionListener(this);

        }

        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(exit);

        bar.add(file);

        add(scroll);
        setJMenuBar(bar);
    }

    public class Layout extends JPanel {

        private JTextField findText;
        private JButton search;
        private JTextArea text;
        private JMenu menu;
        private JMenuItem menuItem;
        private JMenuItem exit;
        private DefaultListModel<String> vieta;
        private DefaultListModel<String> boob;

        public Layout() {
            setLayout(new BorderLayout());
            JPanel boob = new JPanel(new GridBagLayout());
            JPanel kvadratas = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            GridBagConstraints dbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            kvadratas.add(new JLabel("Rasti: "), gbc);
            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            findText = new JTextField(20);
            kvadratas.add(findText, gbc);

            gbc.gridx++;
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 4;
            search = new JButton("Ieskoti");
            kvadratas.add(search, gbc);

            text = new JTextArea();
            JScrollPane scroll = new JScrollPane(text);

            add(kvadratas, BorderLayout.NORTH);

            vieta = new DefaultListModel<>();
            JList list = new JList(vieta);
            add(new JScrollPane(list));
            ActionHandler handler = new ActionHandler();

            search.addActionListener(handler);
            findText.addActionListener(handler);
        }

        public class ActionHandler implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                vieta.removeAllElements();

                String searchText = findText.getText();
                try (BufferedReader reader = new BufferedReader(new     FileReader(new File("sarasas.txt")))) {

                    String text = null;
                    while ((text = reader.readLine()) != null) {

                        if (text.matches(searchText)) {

                            vieta.addElement(text);

                        }

                    }

                } catch (IOException ee) {

                    ee.printStackTrace();
                    JOptionPane.showMessageDialog(Layout.this, "Nera failo", "Error", JOptionPane.ERROR_MESSAGE);

                }
            }
        }

    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Atidaryti")) {
            open();
        }else if(e.getActionCommand().equals("Issaugoti")) {
            save(); 
        } else if(e.getActionCommand().equals("Uzdaryti")) {
            System.exit(0);
        }
    }

    private void open(){
        try{
            BufferedReader atidarymas = new BufferedReader(new FileReader("sarasas.txt"));

            String line;
            while((line = atidarymas.readLine()) != null){
                text.append(line + "\n");
            }

            atidarymas.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }

    }

    private void save(){
        try{
            BufferedWriter irasymas = new BufferedWriter(new FileWriter("sarasas.txt"));  

            irasymas.write(text.getText());
            irasymas.close();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    也许你应该考虑使用这个{{CD1}}代替这个^ {< CD2>},因为这将允许你只使用名称或姓氏搜索。此外,为了使用更改的顺序进行搜索,您应该在代码中处理此问题。虚拟解决方案如下所示,当然会有更好的解决方案:

                public void actionPerformed(ActionEvent e) {
                vieta.removeAllElements();
    
                String searchText = findText.getText();
                String[] words = searchText.split(" ");
    
                StringBuilder sb = null;
                if(words.length > 1) {
                    sb = new StringBuilder();
                    sb.append(words[1]).append(" ").append(words[0]);
                }
    
                try (BufferedReader reader = new BufferedReader(new     FileReader(new File("sarasas.txt")))) {
    
                    String text = null;
                    while ((text = reader.readLine()) != null) {
    
                        if (text.contains(searchText) || (sb != null && text.contains(sb.toString()))) {
    
                            vieta.addElement(text);
    
                        }
    
                    }
    
                } catch (IOException ee) {
    
                    ee.printStackTrace();
                    JOptionPane.showMessageDialog(Layout.this, "No file", "Error", JOptionPane.ERROR_MESSAGE);
    
                }
            }