8 Replies - 248 Views - Last Post: 24 June 2015 - 11:45 AM Rate Topic: -----

#1 Grimspiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-June 15

Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 08:56 AM

Hi. I have 3 textboxes for name,surname and phone number. I want to search text file based on what I write in in the textbox, disregarding if is it lowercase letter or not and also if content has even one letter. Exaple: for John I want to enable do display result whether user types just "Jo","J"JoHn" etc.

Can you please tell me if there is a method or some other way how can I disregard lower/upper letter diffrence in search, and also if based on just some letters?

File fajl =new File("phonebook.txt");
        if (!fajl.exists()) {
            throw new RuntimeException("Phonebook not found.");
        }
        String text=jTextField1.getText();
        try{
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            model.setRowCount(0);
            BufferedReader reader = new BufferedReader(new FileReader(fajl));
            String line;
            while ((line = reader.readLine()) != null)
            {
                if (line.equals(text))
                {
                    model.addRow(line.split("\t") );
                }
            }
            reader.close();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error!");
            e.printStackTrace();
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Searching disregarding whether a capital letter or not

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 11224
  • View blog
  • Posts: 42,255
  • Joined: 27-December 08

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 09:07 AM

You can use the equalsIgnoreCase() method rather than the String equals() method.
Was This Post Helpful? 1
  • +
  • -

#3 Grimspiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-June 15

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 09:17 AM

thanks.And to show if if just first one or two letters are written example: "j","jo"or"john"?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 11224
  • View blog
  • Posts: 42,255
  • Joined: 27-December 08

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 09:23 AM

There is no case-insensitive method to check if a substring is present. You would have to implement this functionality yourself. It might be easiest to convert the String toLowerCase() or toUpperCase() first though, as that would achieve the desired functionality. Note that Strings are immutable, so this would actually create a new String with the changes rather than modifying the original String.
Was This Post Helpful? 0
  • +
  • -

#5 Grimspiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-June 15

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 09:52 AM

Hey what is correct syntax for putting content of 3 textboxes into String array?

String[] text=(jTextField1.getText().split("\t")+jTextField2.getText().split("\t")+jTextField3.getText());



NVM the last one.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 11224
  • View blog
  • Posts: 42,255
  • Joined: 27-December 08

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 09:54 AM

Like so:
String[] text = new String[3];
text[0] = jtextField1.getText();
text[1] = jtextField2.getText();
text[2] = jtextField3.getText();


Was This Post Helpful? 0
  • +
  • -

#7 Grimspiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-June 15

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 10:38 AM

line = reader.readLine()


BufferedReader reads all from text file in form of:
John Doe 9999999999

And it treats it as one line.Can I somehow seperate it to compare with just one TextField?

It's seperated with "\t"
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 11224
  • View blog
  • Posts: 42,255
  • Joined: 27-December 08

Re: Searching disregarding whether a capital letter or not

Posted 23 June 2015 - 11:38 AM

It looks like you're familiar with the String split() method. I'd start there.
Was This Post Helpful? 0
  • +
  • -

#9 Grimspiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-June 15

Re: Searching disregarding whether a capital letter or not

Posted 24 June 2015 - 11:45 AM

Well I found not perfect,but not bad solution. So I put it here:

String text=jTextField4.getText();

String line,bigLetter,exactEntry;
while ((line= reader.readLine()) != null)
            {
                bigLetter=line;
                exactEntry=line;
                if ((line.toLowerCase().indexOf(text) != -1)||(bigLetter.toUpperCase().indexOf(text) != -1)||(exactEntry.indexOf(text) != -1))
                {
                    model.addRow(line.split("\t"));
                }
            }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1