The indexOf method for string searching

Hey everyone, I really need some advice on how to use the indexOf method for searching a string. I'm trying to make a search field for a database I have but I can't get this working. Thanks
deedmans
deedmansAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
antonsigurConnect With a Mentor Commented:
see above
yes....
But this only tells you it was found...
you must figure it out now
0
 
pronaneCommented:
here is an example, its difficult enough to understand though:

public class index {
 public static void main(String args[])
 {
  String s = "aaaaaaaaaa";
  int i = 10000000;
  int n = 0;

  // method #1

  if (args.length > 0 && 
    args[0].compareTo("index") == 0) {
   while (i-- > 0)
    n = s.indexOf('x');
  }

  // method #2

  else {
   while (i-- > 0) {
    int len = s.length();
    n = -1;
    for (int j = 0; j < len; j++) {
     if (s.charAt(j) == 'x') {
      n = j;
      break;
     }
    }
   }
  }
 }
}

0
 
pronaneCommented:
here is a good site that explains it look under string handling:
http://www.netguide.aust.com/tutorials/javatute2/javatute2.html#anchor%205
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
What exactly is the problem?
0
 
deedmansAuthor Commented:
// Assignment 4
// By Mark Gordon
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;


//WINDOW BIT

class LLabel extends Label{
     public LLabel(String text){
          setFont(new Font("Serif",Font.PLAIN,16));
          setBackground(Color.white);
     }
}
     
class LButton extends Button{
     public LButton(String text){
     }
}


     
class Entry{
     String author, title, publisher, year;
     
     
     Entry(String a,String t, String p, String y)
     {
          author = a;
          title = t;
          publisher = p;
          year = y;
     }
}



class Library extends Frame implements ActionListener{
    LButton down, up;
    Panel pan;
    TextField txt;
    LLabel author, title, publisher, year, prompt;
    int n=0, count=0;
    final int MAXENTRY = 100;
    Entry [] details;
   
    public Library()
    {
     setLayout(new GridLayout(5,1));
     author = new LLabel("");
     add(author);
     
     title = new LLabel("");
     add(title);
     
     publisher = new LLabel("");
     add(publisher);
     
     year = new LLabel("");
     add(year);
     
     pan = new Panel(new FlowLayout());
     prompt = new LLabel("");
     prompt.setText("Label Field");
     pan.add(prompt);
     add(pan);
     
     txt = new TextField("Insert Text Here",20);
     pan.add(txt);
     add(pan);
     
     up = new LButton("");
     up.setFont(new Font("Serif",Font.ITALIC,12));
     up.setBackground(Color.blue);
     up.setLabel("Search");
     pan.add(up);
     add(pan);
     up.addActionListener(this);
     
     
    }
   
    public void getLibrary(String f)throws IOException{
         String a, t, y, p;
         BufferedReader file = new BufferedReader(new FileReader(f));
         details = new Entry[MAXENTRY];
         
         a = file.readLine();
         while(a!=null)
         {t = file.readLine();
         p = file.readLine();
         y = file.readLine();
         details[n] = new Entry(a, t, p, y);
         n++;
         count = n;
         a = file.readLine();
    }
    n = 0;
    author.setText(""+details[n].author);
    title.setText(""+details[n].title);
    publisher.setText(""+details[n].publisher);
    year.setText(""+details[n].year);
}


         public void actionPerformed(ActionEvent e){
          if(e.getSource()==up)
              {/*I NEED A METHOD: that would search the catalogue.txt and find a string matching to the string typed in my text field. */
     }
}
}
           

//SECOND NON-WINDOW BIT

public class lab67
{
   
    public static void main(String [] args)throws InterruptedException, IOException{
     
     {
         Library b;
         b = new Library();
         b.setSize(500,400);
         b.setTitle("Database Search");
         b.setVisible(true);
         b.addWindowListener
          (new WindowAdapter()
              { public void windowClosing(WindowEvent e)
               { System.exit(0);}
              });
         b.getLibrary("catalogue.txt");
     }
    }
}

Aplogies for posting so much code. Read the actionPerformed.
0
 
antonsigurCommented:
ok.. if substring is not found in a string, indexOf return -1.

So:
String a = "blha blha Anton blha"
string b = "anton"

1.
if (a.indexOf(b) != -1)
   //Then string b is in string a

If you want to ignore case:

2.
if (a.toLowerCase().indexOf(b.toLowerCase()) != -1)
   //ignore case: String b is in string a


In my example, nr 1 will not run the if block but nr 2 will
0
 
deedmansAuthor Commented:
Thanks antonsiqur, but I get the following error when I compile it:

C:\My Documents\lab67.java:116: Method toLowerCase() not found in class Entry.
            if (details[n].toLowerCase().indexOf(txt.getText().toLowerCase()) != -1)
                                      ^
1 error

Process completed.


Does this mean I have to put a method up the top of my program to declare indexOf and toLowerCase?
0
 
deedmansAuthor Commented:
Thanks antonsiqur, but I get the following error when I compile it:

C:\My Documents\lab67.java:116: Method toLowerCase() not found in class Entry.
            if (details[n].toLowerCase().indexOf(txt.getText().toLowerCase()) != -1)
                                      ^
1 error

Process completed.


Does this mean I have to put a method up the top of my program to declare indexOf and toLowerCase?
0
 
antonsigurCommented:
That's because Entry is not of type String. This is for comparing Strings.

-----
class Entry{
    //added public, you should implement set's and get's instaed
    public String author, title, publisher, year;
   
   
    Entry(String a,String t, String p, String y)
    {
         author = a;
         title = t;
         publisher = p;
         year = y;
    }
}
-----
 if (details[n].author.toLowerCase().indexOf(txt.getText().toLowerCase()) != -1 || details[n].title.toLowerCase().indexOf(txt.getText().toLowerCase()) != -1 && ...)
0
 
antonsigurCommented:
No sorry, && should be || (in the last sentence)
0
 
deedmansAuthor Commented:
Thanks a lot dude :) It works great! Is there any other way to do it without having to do the massive or's?
0
 
antonsigurCommented:
Nopes... you must implement it somewhere...
0
 
deedmansAuthor Commented:
ah ok. I've tried putting it in a for loop since there is more than one database file but unsuccessfully :/ there are 11 different outputs for author,title,publisher and year. But the for(int n=0;n<11;n++){} aint doing the trick. Any ideas?
0
 
antonsigurCommented:
Please explain better
0
 
antonsigurCommented:
If you need to search in multible strings:

Strings[] manyStrings = .... (or many author.title...

boolean found = false;

for (int i = 0 ; i<manyStrings.length() ; i++) {

  if (foundIn(manyStrings[i]) {
    found = true;
    break;
   }
}

if (found) {
  The string was found in some line
}

0
 
deedmansAuthor Commented:
well, n is 0, which is the 1st Entry. How would I code it to search through all 11 entries?
0
 
antonsigurCommented:
by putting it in a for loop, like you just said:

boolean found = false;

for (int i = 0 ; i<manyStrings.length() ; i++) {

 if (details[i].author.toLowerCase().indexOf(txt.getText().toLowerCase()) != -1 || details[i].title.toLowerCase .... {
   found = true;
   break;
  }
}

if (found) {
 The string was found in some line
}
0
 
deedmansAuthor Commented:
Is manyStrings my details?
0
All Courses

From novice to tech pro — start learning today.