0

I am new using java. I wanted to ask, if I have a text file containing different words per line and I want to read that file as a string in order to detect if there are certain words that are written in all caps (abbreviations). The exception being that if the word starts with "#" or and "@" it will ignore counting it. For example I have:

OMG terry is cute #HAWT SMH

The result will be: Abbreviations = 2.

or

terry likes TGIF parties @ANDERSON

The result will be: Abbreviations = 1.

Please help

3 Answers3

0

Try to use the .split(String T) method, and the .contains(char C) methods ..... I think they will help you a lot ....

Function split:

http://www.tutorialspoint.com/java/java_string_split.htm

Function contains:

http://www.tutorialspoint.com/java/lang/string_contains.htm

Duck
  • 26,924
  • 5
  • 64
  • 92
BinaryMan
  • 269
  • 2
  • 6
  • 18
0
    String str1 = "OMG terry is cute #HAWT SMH";
    String str2 = "terry likes TGIF parties @ANDERSON";
    Pattern p = Pattern.compile("(?>\\s)([A-Z]+)(?=\\s)");
    Matcher matcher = p.matcher(" "+str1+" ");//pay attention! adding spaces 
                                        // before and after to catch potentials in 
                                        // beginning/end of the sentence
    int i=0;
    while (matcher.find()) {
        i++; //count how many matches were found
    }
    System.out.println("matches: "+i); // prints 2

    matcher = p.matcher(" "+str2+" ");
    i=0;
    while (matcher.find()) {
        i++;
    }
    System.out.println("matches: "+i); // prints 1

OUTPUT:

matches: 2
matches: 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Here is a bazooka for your spider problem.

(mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
  1. Pad the string with a space (because .split removes trailing empty strings).
  2. Split on the pattern "behind this is neither @ nor #, and this is two or more capital letters". This returns an array of substrings of the that are not part of abbreviations.
  3. Take the length of the array and subtract 1.

Example:

mystring = "OMG terry is cute #HAWT SMH";
String[] arr = (mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
//arr is now {"", " terry is cute #HAWT ", " "}, three strings
return arr.length-1; //returns 2
leewz
  • 3,201
  • 1
  • 18
  • 38