-1

I don't want "charAt(1);" to return the character at position 1 in the string... I want it to report all instances of "matching = 1;"

Take for example

int matching = 1;
int notMatching = 0;
int HI = matching;
int IH = notMatching; 
int searchString = theString.charAt(notMatching);

  int count = 0;
  int index = 0;

      while (0 < theString.length())
      {
      int  = theString.charAt(matching);
        if (theString.charAt(notMatching) == searchString)    
        {
        count = count + 1;
        }
        index = index + 1;
      }

Not a great example but basically what I want, or what this program is supposed to do is take a user input:

HIHIIH

and report the instances of HI spelled as IH... so the return would be for instance;

System.out.println("HI was spelled as IH" + count + "times");

EDIT: The "Duplicate" doesn't help me

Computer
  • 132
  • 11
  • So you want to count the number of occurrences of a substring? That is, you want to know the number of times that "IH" occurs in a string? – Maz Mar 01 '15 at 06:53
  • There is an [`indexOf(String)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)) method. That is what you want to use – Rohit Jain Mar 01 '15 at 06:56
  • Do you think you can write an example? – Computer Mar 01 '15 at 06:56
  • 1
    @Donnie See my answer. It solves your problem. Btw the code you have written just doesn't make sense at all.. – Chetan Kinger Mar 01 '15 at 07:09
  • I'm getting "cannot find Symbol" errors from compiler for your snippet. – Computer Mar 01 '15 at 07:12

1 Answers1

0

You could use StringBuffer's reverse method and Pattern and Matcher as follows :

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchReverseString {
    public static void main(String []args) {
        String str = "HIHIIH";
        String strToSearch = "HI";
        String strToSearchReversed = new StringBuffer(strToSearch).reverse().toString();
        Pattern strPattern = Pattern.compile(Pattern.quote(strToSearchReversed));
        Matcher matcher = strPattern.matcher(str);
        int counter = 0;
        while(matcher.find()) {
            ++counter;
        }
        System.out.println(strToSearch+" was spelt as "+strToSearchReversed+" "+counter+" times");
    }
}

The output is :

HI was spelt as IH 2 times

Note that in my program, str represents the input String and strToSearch represents the String to be searched in reverse order. The use of Pattern.quote ensures that any meta-characters are automatically escaped.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • But I get compiling errors. – Computer Mar 01 '15 at 07:15
  • Well, are you importing the required classes? – Chetan Kinger Mar 01 '15 at 07:15
  • I'm importing "java.util.*" if that's what you're asking? Pattern and Matcher seems incompatible – Computer Mar 01 '15 at 07:17
  • I'm running "java version "1.7.0_60-ea"" – Computer Mar 01 '15 at 07:22
  • Looks like you don't understand how imports work. 'import java.util.*;' is not the same as 'import java.util.regex.*;' Either use the imports I have provided or change import statement to import java.util.regex.* – Chetan Kinger Mar 01 '15 at 07:24
  • those imports weren't there before.... I know what imports are... But thank you for finally including the specific imports required. – Computer Mar 01 '15 at 07:25
  • But say, I wanted user input instead of just searching HIHIIH... – Computer Mar 01 '15 at 07:27
  • java.util.* imports all java utilities... java.regex.* imports all regular expressions... am I wrong? You're kind of wavering my confidence... – Computer Mar 01 '15 at 07:29
  • Enjoy your tick. It's all I can give.. not a seasoned enough user to give up votes – Computer Mar 01 '15 at 07:36
  • It's no problem. You deserve it. You helped me. Maybe when I get to the minimum i'll revisit and give you your deserved up vote. – Computer Mar 01 '15 at 07:38
  • For a bonus, discount spelling errors overlapping correct spellings. – greybeard Mar 01 '15 at 07:39
  • Can I give strToSearch multiple assignments? @bot – Computer Mar 01 '15 at 07:44
  • @Donnie No. But here's a hint. Change strToSearch to an array of strings and the rest of the program will need to be changed accordingly. – Chetan Kinger Mar 01 '15 at 07:47
  • what else should/could I change in accordance with making it an array? I get an error under "new".. says: no suitable constructor found – Computer Mar 01 '15 at 07:51
  • I've changed it to: "String[] strToSearch = {"IH", "AH", "BH", "CH", "TH", "EH", "UH", "HH"}; – Computer Mar 01 '15 at 07:56
  • @Donnie You won't learn this way. Try to solve this problem yourself. – Chetan Kinger Mar 01 '15 at 08:00
  • @Donnie It's really not about the tick. You can unaccept my answer if you like. That's just against the purpose of this webstie. What you are asking for is for me to complete your assignment for you. I can't do that. – Chetan Kinger Mar 01 '15 at 08:09
  • That's not the point. You will face many such situations in the real world where you have to solve a problem that is not in your area of expertise. If you take a shortcut now, you are losing the opportunity to train yourself for such situations. I would say buckle up and take this as a challenge. When you are done with it, you can be proud of yourself and confident that you can tackle problems yourself. I know its easier said than done. But I am speaking out of experience. Take shortcuts now, and you will be taking shortcuts for the rest of your career. – Chetan Kinger Mar 01 '15 at 08:24