[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

Ignore Case data in a String

I have a checkbox. The checkbox asks a user if they want to ignore capitalization. How do I get this data passed from my ItemStateChanged to ignore Capitalization in a string?
0
blazted
Asked:
blazted
  • 8
  • 4
  • 3
  • +1
1 Solution
 
suprapto45Commented:
Hi,

Let say you pass a String, you can use something like.

String passValue = "HI";
String withoutCapitalization = passValue.toLowerCase();

Does it help you?

Regards
Dave
0
 
blaztedAuthor Commented:
This is what I tried. I am searching two strings and when the Checked box is checked I want to search the string regardless of Capitalization.
So i created a mthod to do the searching and then got my values from my text box and my check box. I converted all my string from my text box to uppercase. But the data is not searching right now even after I convert the strings to uppercase.

 // ---- textValueChanged method ----
   public void textValueChanged( TextEvent e ) {
        s1 = Source.getText();
        s2 = Output.getText();
        Search(s1, s2);
        repaint();

        }


   // ---- itemStateChanged method ----
   public void itemStateChanged( ItemEvent e ) {
        if(Capital.getState()== true)
           s1 = s1.toUpperCase();
           s2 = s2.toUpperCase();
           Search(s1, s2);

      System.out.println("Checked");
      System.out.println(s1);
      System.out.println(s2);
      repaint();
   }

// ---- ReadPanel method ----
   private void Search(String c1, String c2 ){
      int start = 0;
        int count = 0;
        int index;

        do{
           index = c2.indexOf(c1, start);
               if (index!=-1) {
                  count++;
                  start=index+1;
           }
           else
              break;
        }
        while (index!=-1);
        System.out.println(count);
        Total = count;
   }

Thanks forthe help.
0
 
suprapto45Commented:
Hi,

Can you send some sample input and output? Thanks.

regards
Dave
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
suprapto45Commented:
Hi,

I saw something....

       if(Capital.getState()== true)
          s1 = s1.toUpperCase();
          s2 = s2.toUpperCase();
          Search(s1, s2);

should be

       if(Capital.getState()== true)
       {
          s1 = s1.toUpperCase();
          s2 = s2.toUpperCase();
          Search(s1, s2);
       }

Regards
Dave
0
 
blaztedAuthor Commented:
Here is the whole Applet for now

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class assig2 extends Applet implements ItemListener, TextListener {
   private Checkbox Capital;
   int Total;
   String s1, s2;
   Label Sub_String;
   Label header;
   Label report;
   TextField Source;
   TextArea Output;
 
   public void init () {
      // set up labels and text boxes
      Sub_String = new Label("Substring");
      header = new Label("Type or Paste a Paragraph");
      Source = new TextField(10);
      Output = new TextArea(5, 20);
      Capital = new Checkbox("Ignore Capitalization");
      report = new Label("There are ");

      // add items to canvas
      add(Sub_String);   add(Source);
      add(header);       add(Output);
      add(Capital);      add(report);

      //Prepare to respond to buttons
      Capital.addItemListener(this);
      Source.addTextListener(this);
      Output.addTextListener(this);

      Total = 0;
   }

   // ---- textValueChanged method ----
   public void textValueChanged( TextEvent e ) {
        s1 = Source.getText();
        s2 = Output.getText();
        Search(s1, s2);
        repaint();

        }


   // ---- itemStateChanged method ----
   public void itemStateChanged( ItemEvent e ) {
        if(Capital.getState()== true){
           s1 = s1.toUpperCase();
           s2 = s2.toUpperCase();
           Search(s1, s2);
      }

      System.out.println("Checked");
      System.out.println(s1);
      System.out.println(s2);
      repaint();
   }

   // ---- actionPerformed method ----
   public void actionPerformed( ActionEvent e ) {
      repaint();
   }

   // ---- paint method ----
   public void paint( Graphics g ) {
      report.setText(Total + "occurences");
   }

   // ---- ReadPanel method ----
   private void Search(String c1, String c2 ){
      int start = 0;
        int count = 0;
        int index;

        do{
           index = c2.indexOf(c1, start);
               if (index!=-1) {
                  count++;
                  start=index+1;
           }
           else
              break;
        }
        while (index!=-1);
        System.out.println(count);
        Total = count;
   }

}

No Matter if my checkbox is ckecked or not the search does not work unless both strings are identical. I tried thje search with s1.equalsIgnoreCase also and I still get the same results.
0
 
suprapto45Commented:
Hi,

I get what you mean....a minute plaese.......


Regards
Dave
0
 
aozarovCommented:
actually I think it should be:
 if(Capital.getState()== true)
{
          s1 = s1.toUpperCase();
          s2 = s2.toUpperCase();
}

Search(s1, s2);
------
But if Capital.getState()== true then it should not matter for this case. (s2 part will be done regardless).
I think you need to call:
s1 = Source.getText();
s2 = Output.getText();
before  if(Capital.getState()== true) also in your itemStateChanged method
And probably in both cases you should declare them as method (instance) variable instead of member variables.

0
 
suprapto45Commented:
Hi,

Somehow, I get endless looping in your codes. Let me check that out.

Regards
Dave
0
 
suprapto45Commented:
Hi,

   // ---- textValueChanged method ----
   public void textValueChanged( TextEvent e ) {
         s1 = Source.getText();
         s2 = Output.getText();
         //Search(s1, s2);
         repaint();

         }

Try to comment the Search(s1, s2) in public void textValueChanged( TextEvent e ). Now, when you check or uncheck the checkbox, your search will be invoked. There would be some result. Is that what you want?

Regards
Dave
0
 
suprapto45Commented:
Hi,

   public void textValueChanged( TextEvent e ) {
      if(Capital.getState()== true)
      {
            s1 = Source.getText().toUpperCase();
            s2 = Output.getText().toUpperCase();
        Search(s1, s2);            
      }
      else
      {
      
         s1 = Source.getText();
         s2 = Output.getText();
         Search(s1, s2);
      }
         //repaint();

         }

Regards
Dave
0
 
gnoonCommented:
blazted,

Your program code is ok except 2 bugs as I can see:

- NullPointerException will thrown when checks the checkbox at the first time.
- You will meet infinite loop when the searching text (c1) is empty.
- Program will search text without ignore Capitalization when changing the text while the checkbox was checked.

To fix them, try this:

- In itemStateChanged() method, change s1.toUpperCase() to Source.getText().toUpperCase(), and s2 also.
- In Search() method, check if c1 is null or empty string or c2 is null, then break out the function before search.
  !!! Don't forget to reset Total value before break out.
- Use the last Dave's code.
0
 
gnoonCommented:
>Your program code is ok except 2 bugs as I can see:
.... shud be 3 bugs ... I cant stop my browser ;-)
0
 
blaztedAuthor Commented:
Ahh, that works perfectly. Thank you A
0
 
blaztedAuthor Commented:
gnool so the search Method should have a check before the statemnet that counts the occurences? Then reset the counter

if   (c1 == null) || (c2 == null)   )
  count = 0;
   break;

Like so?

 
0
 
gnoonCommented:
no, use 'return' instead of 'break' to break out of function.
try this code at the first line of Search()

if( c1==null || c2==null || c1.equals("") )
{
    Total = 0; // reset Total
    repaint(); // show Total
    return;     // break out of function
}
0
 
suprapto45Commented:
Hi,

not like that blazted.

Below is the correct codes not to make your application endless looping.

   public void textValueChanged( TextEvent e ) {
         
      if(Capital.getState()== true)
      {
            if(Source.getText().length() > 0 && Output.getText().length() > 0)
            {
            
            s1 = Source.getText().toUpperCase();
            s2 = Output.getText().toUpperCase();
              Search(s1, s2);
            }            
      }
      else
      {
            if(Source.getText().length() > 0 && Output.getText().length() > 0)
            {      
         s1 = Source.getText();
         s2 = Output.getText();
         Search(s1, s2);
            }
      }
         //repaint();

         }

Regards
Dave
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now