jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes eliminate case sensativeity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "eliminate case sensativeity" Watch "eliminate case sensativeity" New topic
Author

eliminate case sensativeity

jim, valenta
Ranch Hand

Joined: Mar 16, 2001
Posts: 47
Hi,
I have a simple search but wanted to modify it so that its not case sensative and, if possible, so it will run an * wildcard. Does anyone know how to do either of these??
jim
public class Project1 extends java.applet.Applet {
int selectedIndex[] = {0, 1, 2, 3};
String FirstNames[] = {"Teodora", "Andreea", "Anca", "Andrei", "Emil", "Vlad", "Daniel", "Vanda", "Simina", "Nicoleta", "Teodora", "Ioana"};
String LastNames[] = {"Szeplachi", "Weisl", "Lasc", "Jorza", "Popa", "Valceanu", "Trusca", "Stan", "Iov", "Pavel", "Vente", "Stan"};
String Phone[] = {"253404", "259531", "259903", "289732", "252120", "256075", "268963", "221856", "269749", "219159", "222458", "232293"};
String Address[] = {"Frasinului", "Bradului", "Viitorului", "Progresului", "Blajului", "Bradului", "Viitorului", "Ciresului", "Crinilor", "Progresului", "Blajului", "Visinului"};
String complete[] = {"Teodora Szeplachi, 253404, Frasinului Street, no 10",
"Andreea Weisl, 259531, Bradului Street, no 98",
"Anca Lasc, 259903, Viitorului Street, no 8",
"Andrei Jorza, 289732, Progresului Street, no 15",
"Emil Popa, 252120, Blajului Street, no 3",
"Vlad Valceanu, 256075, Bradului Street, no 7",
"Daniel Trusca, 268963, Viitorului Street, no 11",
"Vanda Stan, 221856, Ciresului Street, no 56",
"Simina Iov, 269746, Crinilor Street, no 34",
"Nicoleta Pavel, 219159, Progresului Street, no 45",
"Teodora Vente, 222458, Blajului Street, no 12",
"Ioana Stan, 232293, Visinului Street, no 11A"};
Font f=new Font("TimesRoman", Font.BOLD + Font.ITALIC, 12);
String result1 = new String();
Label criteria = new Label("Search Criteria: ", Label.CENTER);
Choice searchlist = new Choice();
Label forLabel = new Label("Search for: ", Label.CENTER);
TextField textfield = new TextField("Enter you search keyword here!", 50);
Button search = new Button("SEARCH");
Button reset = new Button("RESET");
Label results = new Label("Search Results:");
TextArea resultsdisplay = new TextArea(4, 150);
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;}

public void init () {
setFont(f);
//setColor(Color.blue);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);

//Search Criteria Label
buildConstraints(constraints, 0, 0, 2, 1, 0, 10);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(criteria, constraints);
add(criteria);

//Search Choicelist
buildConstraints(constraints, 0, 1, 2, 1, 0, 10);
constraints.anchor = GridBagConstraints.CENTER;
gridbag.setConstraints(searchlist, constraints);
searchlist.addItem(" by First Name ");
searchlist.addItem(" by Last Name ");
searchlist.addItem(" by Phone Number ");
searchlist.addItem(" by Street Name ");
add(searchlist);

//Search For Label
buildConstraints(constraints, 0, 2, 2, 1, 0, 10);
constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(forLabel, constraints);
add(forLabel);

//Search Textfield
buildConstraints(constraints, 0, 3, 2, 1, 0, 10);
constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(textfield, constraints);
add(textfield);

//Search Button
buildConstraints(constraints, 0, 4, 1, 1, 50, 10);
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(search, constraints);
add(search);

//Reset Button
buildConstraints(constraints, 1, 4, 1, 1, 50, 0);
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(reset, constraints);
add(reset);

//Results Label
buildConstraints(constraints, 0, 5, 2, 1, 0, 10);
constraints.anchor = GridBagConstraints.SOUTHWEST;
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(results, constraints);
add(results);

//Results Display TextArea
buildConstraints(constraints, 0, 6, 2, 1, 0, 40);
constraints.anchor = GridBagConstraints.SOUTH;
constraints.fill = GridBagConstraints.BOTH;
gridbag.setConstraints(resultsdisplay, constraints);
resultsdisplay.setEditable(false);
add(resultsdisplay);
}

public Insets insets(){
return new Insets(10, 30, 10, 30);}

public void paint(Graphics screen) {
setBackground(Color.white);
screen.setColor(Color.blue);
screen.drawRect(2, 2, 245, 295);
}

public boolean action(Event evt, Object arg) {

if (evt.target instanceof Button) {
handleButton(evt);
return true;}
else return false;}

public void handleButton(Event evt) {

if (evt.target == search){
displayResults();
}
else if (evt.target == reset){
reset();}
}

public void reset(){
searchlist.select(0);
textfield.setText("");
resultsdisplay.setText("No search entered!");
}
public void displayResults(){
int selectedItem = searchlist.getSelectedIndex();

if (selectedItem == selectedIndex[0]){
handleFirstName();}
else if (selectedItem == selectedIndex[1]){
handleLastName();}
else if (selectedItem == selectedIndex[2]){
handlePhone();}
else if (selectedItem == selectedIndex[3]){
handleAdress();}
else{

searchlist.getSelectedItem();
result1 = textfield.getText();
resultsdisplay.setText(result1);}
}

public void handleFirstName(){
String results = textfield.getText();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < FirstNames.length; i++){
if(FirstNames[i].equals(results)){
buf.append(complete[i] + "\n");}
else{ }
}
resultsdisplay.setText(buf.toString());
}

public void handleLastName(){
String results = textfield.getText();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < LastNames.length; i++){
if(LastNames[i].equals(results)){
buf.append(complete[i] + "\n");}
else{ }
}
resultsdisplay.setText(buf.toString());
}

public void handlePhone(){
String results = textfield.getText();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < Phone.length; i++){
if(Phone[i].equals(results)){
buf.append(complete[i] + "\n");}
else{ }
}
resultsdisplay.setText(buf.toString());
}

public void handleAdress(){
String results = textfield.getText();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < Address.length; i++){
if(Address[i].equals(results)){
buf.append(complete[i] + "\n");}
else{ }
}
resultsdisplay.setText(buf.toString());
}
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hmmm... your code would be a lot easier to read (and thus, more likely to get useful responses from readers) if it were indented properly. Use the [ code ] tag to maintain indentation; read about it in our FAQ (see question 4). Also, we really don't need all the GUI code here - your question only relates to the search utility; that's the only part we need to see.
Anyway, I eventually found that you were identifying a match using the String equals() method. The fix is simple - use equalsIngoreCase() instead. This is hardly the most efficient way to search, if the list of data is large, but it's easy enough to implement.
To get wildcard capability though - that's more complex. You could implement something yourself, using String methods like startsWith() and endsWith(), but this gets complicated pretty quickly. (It might be a good idea to prohibit users from entering more than one wildcard in a string - that will reduce the complexity for you somewhat.) Note that startsWith() and endsWith() don't come with ignoreCase versions, so instead you'll probably want to convert all strings to lower case (or upper case if you prefer) before you do any searching.
Alternately, it may be better or easier to find an existing regex package which contains this functionality, and incorporate it into your code. Using google I found a nice collection of related links here. Worth looking into...

"I'm not back." - Bill Harding, Twister
jim, valenta
Ranch Hand

Joined: Mar 16, 2001
Posts: 47
jim thanks. I really appreciate it. Unfortunately I tried your link but it was really bad, barely a link that worked. I will try some more searching but I have done quite a bit and most searches dont work on just HTML and text documents only.
 
 
subject: eliminate case sensativeity
 
Similar Threads
Put an Image into a Grid Bag Layout
Align JLabel, JTextField inside TitledBorder
JComboBox not working properly on a GridBagLayout ??
GridBagConstraints
AWT textfield behave weird when run using MicroSoft JVM