Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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 - Fatal编程技术网

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

JAVA从文本文件中找到几个单词或短语并显示出来,java,Java,当我反向/反向输入词组时,我在查找词组时遇到了一些问题。例如我在找约翰·洛夫洛克的名字和姓氏。程序找到短语并显示给我。当我进入Lovelock John时,它没有找到匹配的对象。我怎样才能获得这种功能 以下是我得到的: public class Programa extends JFrame implements ActionListener { private JTextArea text; public static void main(String[] args) {

当我反向/反向输入词组时,我在查找词组时遇到了一些问题。例如我在找约翰·洛夫洛克的名字和姓氏。程序找到短语并显示给我。当我进入Lovelock John时,它没有找到匹配的对象。我怎样才能获得这种功能

以下是我得到的:

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();
        }
    }
}
公共类程序扩展JFrame实现ActionListener{
私有区域文本;
公共静态void main(字符串[]args){
新程序a().setVisible(true);
}
公共计划a(){
超级(“Redagavimas”);
设置大小(600600);
可设置大小(假);
setDefaultCloseOperation(关闭时退出);
初始化();
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame(“Paieska sarase”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新布局());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共无效初始化(){
text=新的JTextArea();
JScrollPane scroll=新的JScrollPane(文本);
JMenuBar=新的JMenuBar();
JMenu文件=新JMenu(“Failas”);
JMenuItem open=新的JMenuItem(“Atidaryti”);
JMenuItem保存=新的JMenuItem(“Issaugoti”);
JMenuItem exit=新的JMenuItem(“Uzdaryti”);
JMenuItem[]项={打开、保存、退出};
对于(JMenuItem:items){
item.addActionListener(本);
}
文件。添加(打开);
文件。添加(保存);
addSeparator()文件;
文件。添加(退出);
添加(文件);
添加(滚动);
setJMenuBar(bar);
}
公共类布局扩展了JPanel{
私有JTextField findText;
私有按钮搜索;
私有区域文本;
私人菜单;
私有项目菜单项;
私人物品出口;
维塔私营企业;
私人模型乳房;
公共布局(){
setLayout(新的BorderLayout());
JPanel boob=newjpanel(newgridbagloayout());
JPanel kvadratas=newjpanel(newgridbaglayout());
GridBagConstraints gbc=新的GridBagConstraints();
GridBagConstraints dbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
gbc.插图=新插图(2,2,2,2);
kvadratas.add(新JLabel(“Rasti:”),gbc);
gbc.gridx++;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;
findText=新的JTextField(20);
kvadratas.add(findText,gbc);
gbc.gridx++;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=4;
搜索=新的JButton(“Ieskoti”);
kvadratas.add(搜索,gbc);
text=新的JTextArea();
JScrollPane scroll=新的JScrollPane(文本);
添加(kvadratas,BorderLayout.NORTH);
vieta=新的DefaultListModel();
JList列表=新的JList(vieta);
添加(新的JScrollPane(列表));
ActionHandler=新的ActionHandler();
addActionListener(处理程序);
findText.addActionListener(处理程序);
}
公共类ActionHandler实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
vieta.removeAllElements();
字符串searchText=findText.getText();
try(BufferedReader=new BufferedReader(new FileReader(新文件(“sarasa.txt”))){
字符串文本=空;
而((text=reader.readLine())!=null){
if(text.matches(searchText)){
vieta.增编(文本);
}
}
}捕获(IOEE异常){
ee.printStackTrace();
JOptionPane.showMessageDialog(Layout.this,“Nera failo”,“Error”,JOptionPane.Error\u消息);
}
}
}
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(例如getActionCommand().equals(“Atidaryti”)){
open();
}else if(例如getActionCommand().equals(“Issaugoti”)){
save();
}else if(例如getActionCommand().equals(“Uzdaryti”)){
系统出口(0);
}
}
私隐无效公开(){
试一试{
BufferedReader atidarymas=新的BufferedReader(新文件阅读器(“sarasa.txt”);
弦线;
而((line=atidarymas.readLine())!=null){
text.append(第+行“\n”);
}
atidarymas.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有void save(){
试一试{
BufferedWriter irasymas=新的BufferedWriter(新文件编写器(“sarasas.txt”);
write(text.getText());
irasymas.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}

< /代码> 也许你应该考虑使用这个<代码>(text。包含(搜索文本))<代码>,而不是这个<代码>(text .Matx.Script文本)< /C> >,因为这将允许你只使用名字或姓氏搜索。此外,为了使用更改的顺序进行搜索,您应该在代码中处理此问题。虚拟解决方案如下所示,当然会有更好的解决方案:

            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);

            }
        }

这似乎是一个隐秘的代码请求。所以它不是一个代码提供者,而是一个问答网站。向我们展示您为解决问题所做的工作,我们将尽力帮助您