[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java String Pattern Search and Count

Posted on 2006-11-08
8
Medium Priority
?
2,292 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
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.

 
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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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