Solved

Java String Pattern Search and Count

Posted on 2006-11-08
8
2,290 Views
Last Modified: 2012-08-13
Hello everyone.  I'm currently looking to search an htm file looking for the number of iterations of strings returned in the file.

For some reason, everytime I run the compiled version; it doesn't count the number of times the string is found.  
I've modified this code for my application, but even this variant on the web isn't working correctly.  Please tell me what I'm doing wrong.

import java.io.*;
import java.net.*;


public class fastsearch
{

      // entry point
      //
      
      public static void main(String[] arguments)
      {
            System.out.println ("hello");

            fastsearch p = null;
            p = new fastsearch();
            p.Test();
      }

      public fastsearch()
      {
      }

      public void Test()
      {
            System.out.println ("Test");
            String szfile = lecture("USERCTRL.htm",false);

            long dwStart = System.currentTimeMillis();

            testcase(szfile,"my pattern",100);
            testcase(szfile,"IPropertyBag",100);
            testcase(szfile,"WebClient",100);
            testcase(szfile,"developer",100);
            testcase(szfile,"the",100);

            long dwEnd = System.currentTimeMillis();

            long dwDuration = dwEnd - dwStart;
            System.out.println ("duration = " + dwDuration + " milliseconds");

      }


      public void testcase(String s, String pattern, int nbtimes)
      {
            for (int k = 0; k < nbtimes; k++)
            {
                  int nb = 0;
                  int i,j;
                  i = 0;
                  while ( (j = s.indexOf(pattern,i))> -1 )
                  {
                        i = j + pattern.length();
                        nb++;
                  }

      /*            if (k==0)
                        System.out.println (nb + " occurences");*/
            }
      }


      public String lecture (String nom, boolean bKeepEOL)
      {

            String szTemp="";

            // build a EOL string
            byte[] myEOL= { 0x0D, 0x0A };
            String szEOL=new String(myEOL);

            InputStream is=null;

            try
            {
                  // il s'agit d'ouvrir le fichier en extrayant d'abord le chemin d'accès complet
                  String s=new String(System.getProperty("user.dir")); // propriété système "user.dir"
                  
                  // for DEBUG
                  System.out.println ("System property user dir = " + s);

                  // puis en transformant les anti-slashs de MS-DOS en slashs d'Unix puis en passant
                  // l'adresse URL complète protocole="file:"  + séparateur="//"
                  // + host=<void> + séparateur="/"
                  // + file="c:/jdk1.1.4/code/compta"
                  // + un séparateur + un nom quelconque (arst.html) qui va être viré automatiquement
                  // par le générateur d'URL et remplacé par le fichier à ouvrir dans l'URL réel.
                  //URL mon_url=new URL("file:///c:/jdk1.1.4/code/compta/arst.html");
                  URL mon_url=new URL("file:///"+s.replace('\\','/')+"/USERCTRL.html"); // s.replace('\\','/')

                  is = new URL(mon_url, nom).openStream();
                  // for DEBUG
                  //if (is!=null) System.out.println ("Ouverture du fichier \""+nom+"\"\n");

                  // ici lecture effective du flux par lignes entières
                  BufferedReader d = new BufferedReader(new InputStreamReader(is));

                  while (d.ready()) // on continue à lire tant qu'il y a des enregistrements à lire
                  {
                        // ligne 1= description entité, ligne 2= son nom
                        szTemp += d.readLine();
                        if (bKeepEOL)
                        {
                              szTemp += szEOL;
                        }
                  }

            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }

            try
            {
                  if (is != null)
                  is.close();
            }
            catch(Exception e)
            {
            }

            return szTemp;

      } // end point

}

0
Comment
Question by:jwphillips80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17899100
I don't understand testcase. Why is there an 'nbtimes' value - what is it? why is this

>>
/*          if (k==0)
                    System.out.println (nb + " occurences");*/
>>


commented out?
0
 
LVL 8

Author Comment

by:jwphillips80
ID: 17899968
Sorry, that shouldn't be commented out.  The code is something I saw online that I've modified some of the strings for searching my own files.  the nbtimes variable is the loop maximum for rechecking the file.  

My understanding of this is fairly limited, so that's why I'm here.

John
0
 
LVL 8

Author Comment

by:jwphillips80
ID: 17900055
I would actually take any code that does this as I'm trying to streamline some testing of files that we have here at the office.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 86

Expert Comment

by:CEHJ
ID: 17900210
StreamTokenizer is what you need really. See

http://www.cs.hut.fi/Docs/Eckel/TIJ2ed/code/c11/WordCount.java
0
 
LVL 2

Accepted Solution

by:
saintsairforce earned 200 total points
ID: 17973802
Here is a simple program that can do what you want. You'll have to modify it to work for your program.

import javax.swing.*;
import java.io.*;
import java.io.File;

public class countString
{
      public countString()
      {
            File inputFile = new File("test.txt");
            System.out.println("The word Hello is in the file test.txt "+stringCounter(inputFile,"hello")+" times");      
      }      
      
      public int stringCounter(File fileName,String searchString)
      {
            int counter=0;
            
            try
            {
                  BufferedReader input = new BufferedReader(new FileReader(fileName));
              String line="";
              
              while ((line = input.readLine()) != null)
              {
                      //fills array with individual words
                      String temp[] = line.split(" ");
                
                        for(int cnt=0;cnt<temp.length;cnt++)
                        {
                              /*case sensative code
                              if(temp[cnt].equals(searchString))
                                    counter++;
                              */      
                              
                              //non case senseative
                              if((temp[cnt].toUpperCase()).equals(searchString.toUpperCase()))
                                    counter++;
                        }
              }
              
              input.close();
        }
        catch(Exception e)
        {
        }
      
            return counter;
      }
      
      public static void main(String args[])
      {
            new countString();      
      }
}

Cheers,
Ricky
0
 
LVL 2

Expert Comment

by:saintsairforce
ID: 17973805
Note, you should use stringtokenizer instead of the split command I used in order to get it to pick up words that have special caracters attached to them. I didn't want to give you the complete ansewer as this is simular to a lot of homework problems.
0
 
LVL 8

Author Comment

by:jwphillips80
ID: 18172725
Thanks.  I modified the code, got the parsing I wanted, and can now speed up some of my testing processes.

J
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question