简体   繁体   中英

Searching string from text file(case insensitive)

I am doing my thesis in Java. I am stuck at one point. I can't make work search String from file. I need to search user inputted word from file even if the String from file is "F" and he enters "f" I gotta get response. What is the best method to do this? And is this even best way to achieve this kind of thing? Maybe it is better to use ArrayList correct me if I am wrong.

This is what I've done so far:

 JButton btnTuletaParoolMeelde = new JButton("Tuleta parool meelde");
 btnTuletaParoolMeelde.setBackground(new Color(37, 209, 175));
 btnTuletaParoolMeelde.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {


            //Search
            String keskkond = textArea.getText();

            if(keskkond.length()<=0){
                System.out.println("You didn't enter anything");
            }else{
               int tokencount;
               FileReader filereader = null;
            try {
                filereader = new FileReader("paroolid.txt");
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
               BufferedReader br=new BufferedReader(filereader);
               String s;
               int linecount=0;

               String keyword = keskkond; //keskkond is like Facebook etc..

               try {
                while ((s = br.readLine())!=null){
                      if(s.contains(keyword))
                     System.out.println(s);

                   }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }                                                                             
               }
   }

This is where person can input network and his password and then it will write it into file.

  //Add your password and network and Button actions
  btnLisaParool = new JButton("Add passowrd");
  btnLisaParool.setBackground(new Color(37, 209, 175));
  btnLisaParool.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {




    String parool = String.valueOf(passwordField.getPassword());
    String koht = textField.getText();
    if (koht.isEmpty() && parool.isEmpty()) {
     lblFeedback.setText("You didn't enter anything.");
    } else if(parool.isEmpty()) {
        lblFeedback.setText("Enter password please!.");
    }else if(koht.isEmpty()){
        lblFeedback.setText("Enter network please!");
    }else{
     FileWriter fWriter = null;
     BufferedWriter writer = null;
     try {
      fWriter = new FileWriter("paroolid.txt", true);
      writer = new BufferedWriter(fWriter);
      writer.write("Teie " + koht + " -i parool oli: " + parool);
      writer.newLine();
      writer.close();
      lblFeedback.setText("Teie " + koht + " parool, mis koosnes " + parool.length() + " tähest, on salvestatud.");

     } catch (Exception e1) {
         lblFeedback.setText("Error: " + "Couldn't find file");
     }

    }
    passwordField.setText("");
    textField.setText("");
   }

First, don't execute long running operations on the EDT (like reading/writing files). Second, make sure you release all the resources that you use to prevent memory leaks. To answer your question, you can modify both the String you're searching for and the one you're searching through to lower or upper case.

Here is an example:

try(BufferedReader br = new BufferedReader(new FileReader("paroolid.txt"))){
    for(String line; (line = br.readLine()) != null;){
        if(line.toLowerCase().contains(toSearchFor.toLowerCase())){
            System.out.println(line);
        }
    }
}catch(Exception e){
    e.printStackTrace();
}

首先检查“ f”,然后检查toUppercase(“ f”)

equalsIgnoreCase() method from string can solve your problem. Or you can use

org.apache.commons.lang3.StringUtils.containsIgnoreCase()

If you have to use contains.

Using equalsIgnoreCase will make it so that it doesn't take the case of the letter into account when testing to see if they are equal.

Also, even though this wasn't asked, I have two suggestions:

Instead of:

try {
     filereader = new FileReader("paroolid.txt");
     } catch (FileNotFoundException e2) {
       // TODO Auto-generated catch block
       e2.printStackTrace();
            }

try doing this:

try ( br = new BufferedReader(new FileReader("paroolid.txt"))){
         } catch (FileNotFoundException e2) {
           // TODO Auto-generated catch block
           e2.printStackTrace();
                }

This is called a try-with-resources statement and will automatically close the filereader. The same can be done for your BufferedReader.

The second suggestion is to just use the Scanner class. Unless you were explicitly using this class, it is better to just use the Scanner class. In your case, it won't have any disadvantages and is easier to use.

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