Solved

Java String Pattern Search and Count

Posted on 2006-11-08
8
2,285 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 77
array11 challenge 16 52
@SBGen Method 3 25
Java Timer (static) 9 19
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now