简体   繁体   中英

How do i search for a string in file using buffered reader

How do I make the program search a file for a string and then print out the line? What I'm trying to do is create an address book type of thing. I'm sorry if my code is messy as it's the first time I've ever done this before. Would a HashMap be better for this kind of thing? If so is it possible to have more than one value attached to the same key?

public class contats extends JFrame {
    JLabel nameLabel;
    JLabel phoneLabel;
    JLabel notesLabel;
    JLabel searchLabel;
    JTextField name;
    JTextField phone;
    JTextField notes;
    JTextField searchField;
    JButton add;
    JButton search;

        public contats() {
        setLayout(new FlowLayout());

        nameLabel = new JLabel("Name: ");
        add(nameLabel);

        name = new JTextField(15);
        add(name);

        phoneLabel = new JLabel("Number: ");
        add(phoneLabel);

        phone = new JTextField(15);
        add(phone);

        notesLabel = new JLabel("Notes: ");
        add(notesLabel);

        notes = new JTextField(10);
        add(notes);

        add = new JButton("Add Contact");
        add(add);

        searchLabel = new JLabel("search");
        add(searchLabel);

        searchField = new JTextField(15);
        add(searchField);

        search = new JButton("Search");
        add(search);

        event e = new event();
        add.addActionListener(e);
        search.addActionListener(e);


    }

       public class event implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){
            try {
                String[] con = {name.getText(),phone.getText(),notes.getText()};
                BufferedWriter bw = new BufferedWriter(new FileWriter("peewee.txt", true));
                for(String s : con){
                    bw.newLine();
                    bw.write(s);
                }
                bw.close();

            }catch(Exception ex){
                JOptionPane.showMessageDialog(null,  "blerr");
            }

        } else if(e.getSource() == search){
            try{
                String input = search.getText();
                String file;
                String searchArray[];

                BufferedReader br = new BufferedReader(new FileReader ("peewee.txt"));
                while((file = br.readLine())!= null){ // this is where i need the help??
                    if(file == input){
                    System.out.println(input);  
                    }
                }
                br.close();

            } catch(Exception ex) {
                JOptionPane.showMessageDialog(null, "sdfadsfsdf");

            }
        }
    }

}

    public static void main(String[] args) {
        contats gui = new contats();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 300);
        gui.setTitle("Cobtacts");
        gui.setVisible(true);



    }
}

use

better, nicer, faster, could be an idea

  • your description, code posted talking about multiple of searching field (JTextField), then to read File to the DefaultTableModel and use built_in RowFilter in JTable , (all parts of this point were many times asked and answered here), then there is possible to searching from two or more JTextField continiously (required little bit effort moreover)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM