Results 1 to 3 of 3
  1. #1
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Question How To count the occurnces of astring in another string

    Hello , How To count the occurnces of astring in another string I try This Code but the result is wrong help please
    Java Code:
    public class Main {
     static Scanner  console = new Scanner(System.in);
        public static void main(String[] args)
        {
            System.out.println("Enter Two stings ");
            System.out.println();
            String str1 = console.nextLine();
            String str2 = console.nextLine();
           print (count (str1,str2 ));  
        }       
        public static int  count (String x, String y)
        {
        int num = x.replaceAll(x ,y).length();
        return(num);
        }        
        public static void  print (int b)
        {
        System.out.println("The Number of occrrunces is " +b);
        }
    }

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    10

    Default

    Just to confirm,

    Can you show the example inputs and an output?
    freedom exists in the world of ideas

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    9

    Default

    Here is a classical implementation:

    Java Code:
    package javaforums;
    
    public class StringCount
    {
        public static void main(String[] args)
        {
            String source = "String having a lot of instances of the substring in";
            String query = "in";
            int occ = 0;
    
            int pos = source.indexOf(query);
            while (pos != -1)
            {
                System.out.println("Query string [" + query + "] found at index: " + pos);
                occ++;
                pos = source.indexOf(query, pos + query.length());
            }
    
            System.out.println("Query string [" + query + "] found: " + occ + " times");
        }
    }
    When you run it, it will display:
    Java Code:
    Query string [in] found at index: 3
    Query string [in] found at index: 10
    Query string [in] found at index: 23
    Query string [in] found at index: 46
    Query string [in] found at index: 50
    Query string [in] found: 5 times
    You can adapt it to your requirements so you read the source and query strings from the console. Enjoy.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:02 AM
  2. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  3. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM
  4. how to count 2 inserts together?
    By kim85 in forum New To Java
    Replies: 0
    Last Post: 01-20-2008, 12:25 PM
  5. Getting row count from executeQuery()
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •