String count with substring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sang
    New Member
    • Sep 2006
    • 83

    String count with substring

    Hello
    i have a doubt in string count with the sub string.
    the main string is "java,mysql,jsp ,java,html";
    the sub string is like this "java,mysql "

    The output is like this
    java 2
    mysql 1

    that is count the no of occureience of the sub string .

    how can i do this please help to do this .

    Thanks
    Sang
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Could you please explain your problem a bit more fully and post any code you've written or ideas you have?

    Comment

    • sang
      New Member
      • Sep 2006
      • 83

      #3
      Here is my code
      Code:
      import java.io.*; 
       import java.util.StringTokenizer; 
       public class strcount
          {  	
                public static void main(String arg[]) {  	
      	 String Resume = "java,html,jsp,c,java,dhtml,unix,html";  		          String Keyword = "java,html";  		 
                StringTokenizer st = new      StringTokenizer(Keyword, ",");  		 
                int count = st.countTokens();  		 
                int pos = -1;  
      	 while ( (pos = Resume.indexOf( Keyword, pos + 1 ) ) > -1 )   			 count++ ;
      	System.out.println( "The Keyword" + Keyword + " occurs " + count +               " times" ) ; 
       	 }   
      }
      The output is The Keywordjava,htm l occurs 3 times.

      but i want to print java 2 times
      html 1 time
      Help me please
      Thank you
      Sang

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sang
        Hello
        i have a doubt in string count with the sub string.
        the main string is "java,mysql,jsp ,java,html";
        the sub string is like this "java,mysql "

        The output is like this
        java 2
        mysql 1

        that is count the no of occureience of the sub string .

        how can i do this please help to do this .

        Thanks
        Sang
        You better have a look at regular expressions too.

        Comment

        • sang
          New Member
          • Sep 2006
          • 83

          #5
          Originally posted by r035198x
          You better have a look at regular expressions too.
          please give me the code to solve my problem

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by sang
            please give me the code to solve my problem
            The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

            Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

            Then when you are ready post a new question in this thread.

            MODERATOR

            That is in our Posting Guidelines. Not only that, but we can't help you without more information (which a more specific question would have given us). It depends what you are looking for, and what your regex is currently. Try looking at this tutorial, and see where it gets you. When you have another, more specific question, post it here, and we'll be able to help you better.

            Comment

            • sang
              New Member
              • Sep 2006
              • 83

              #7
              Originally posted by sicarie
              The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

              Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

              Then when you are ready post a new question in this thread.

              MODERATOR

              That is in our Posting Guidelines. Not only that, but we can't help you without more information (which a more specific question would have given us). It depends what you are looking for, and what your regex is currently. Try looking at this tutorial, and see where it gets you. When you have another, more specific question, post it here, and we'll be able to help you better.

              Here is my example code i already posted.so please correct my code to solve my problem.
              Re: String count with substring
              Here is my codeCode:
              import java.io.*;
              import java.util.Strin gTokenizer;
              public class strcount
              {
              public static void main(String arg[]) {
              String Resume = "java,html,jsp, c,java,dhtml,un ix,html"; String Keyword = "java,html" ;
              StringTokenizer st = new StringTokenizer (Keyword, ",");
              int count = st.countTokens( );
              int pos = -1;
              while ( (pos = Resume.indexOf( Keyword, pos + 1 ) ) > -1 ) count++ ;
              System.out.prin tln( "The Keyword" + Keyword + " occurs " + count + " times" ) ;
              }
              }



              The output is The Keywordjava,htm l occurs 3 times.

              but i want to print java 2 times
              html 1 time
              Help me please
              Thank you
              Sang

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by sang
                Here is my example code i already posted.so please correct my code to solve my problem.
                You are better off using regular expressions for this. If you do have to use that tokenizer of yours, then you should use something like

                Code:
                 
                String resume = "java,html,jsp,c,java,dhtml,unix,html";
                StringTokenizer st = new StringTokenizer(resume , ",");
                int javaCount = 0;
                int htmlCount = 0;
                while(st.hasMoreTokens()) {
                 String s = st.nextToken();
                	if(s.equals("java")) {
                		javaCount = javaCount  + 1;
                	}
                	else if(s.equals("html")) {
                		htmlCount =  htmlCount + 1;
                	}
                }

                Comment

                • sang
                  New Member
                  • Sep 2006
                  • 83

                  #9
                  Originally posted by r035198x
                  You are better off using regular expressions for this. If you do have to use that tokenizer of yours, then you should use something like

                  Code:
                   
                  String resume = "java,html,jsp,c,java,dhtml,unix,html";
                  StringTokenizer st = new StringTokenizer(resume , ",");
                  int javaCount = 0;
                  int htmlCount = 0;
                  while(st.hasMoreTokens()) {
                   String s = st.nextToken();
                  	if(s.equals("java")) {
                  		javaCount = javaCount  + 1;
                  	}
                  	else if(s.equals("html")) {
                  		htmlCount =  htmlCount + 1;
                  	}
                  }
                  Sorry to distrub you.
                  I am not finished my work till. From the above coding we don't use the sub string.
                  My actual requriement is count the no of substrings occurs in the main string.

                  my actual string is "java,html,unix ,java"
                  my substring is "java,html"

                  output
                  java occures in 2 times
                  html occures in 1 time

                  the output we count the no of occurence of java and html.

                  the sub string is not only java and html,anything in the actual string will be given.

                  So help to do this.Its very urgent please do the needful

                  Thanks in Advance
                  Sang

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by sang
                    Sorry to distrub you.
                    I am not finished my work till. From the above coding we don't use the sub string.
                    My actual requriement is count the no of substrings occurs in the main string.

                    my actual string is "java,html,unix ,java"
                    my substring is "java,html"

                    output
                    java occures in 2 times
                    html occures in 1 time

                    the output we count the no of occurence of java and html.

                    the sub string is not only java and html,anything in the actual string will be given.

                    So help to do this.Its very urgent please do the needful

                    Thanks in Advance
                    Sang

                    How about this?

                    Code:
                     
                    int count = 0;
                      int wordLength = word.length();
                      for(int i = 0; i < sentence.length(); i = i + wordLength) {
                       if(word.equals(sentence.substring(i, i + wordLength))) {
                    	count++;
                       }
                      }
                    Let me know where you don't understand.

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Originally posted by r035198x
                      How about this?

                      Code:
                       
                      int count = 0;
                        int wordLength = word.length();
                        for(int i = 0; i < sentence.length(); i = i + wordLength) {
                         if(word.equals(sentence.substring(i, i + wordLength))) {
                      	count++;
                         }
                        }
                      Let me know where you don't understand.
                      I don't think this will work correctly. You are checking every wordLength characters to see if that substring is the word, but the words can be of varying lengths, and you may be checking part of one word and part of another. Why not try:

                      Code:
                      int count = 0;
                      int wordLength = word.length();
                      for (int i = 0; i < sentence.length() - wordLength; i++) {
                         if (word.equals(sentence.substring(i, i + wordLength)
                            count++;
                      }
                      This will check every possible location of word inside the string.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Ganon11
                        I don't think this will work correctly. You are checking every wordLength characters to see if that substring is the word, but the words can be of varying lengths, and you may be checking part of one word and part of another. Why not try:

                        Code:
                        int count = 0;
                        int wordLength = word.length();
                        for (int i = 0; i < sentence.length() - wordLength; i++) {
                        if (word.equals(sentence.substring(i, i + wordLength)
                        count++;
                        }
                        This will check every possible location of word inside the string.
                        Guess I overdid there that time.
                        Thanks again Ganon.

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          That's why we're a team :)

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Ganon11
                            That's why we're a team :)
                            If you only you guys stay around here more often. Our times seem to be off too.
                            You always seem to appear around 4pm (for me here).

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              Right now according to my time, it's 11:22 AM (EST) - what time is it there?

                              I'm usually on around 3:00 PM (EST) and at least once during the day from Monday-Friday.

                              Comment

                              Working...