Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-04-2008, 02:35 PM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
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
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-04-2008, 07:22 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Just to confirm,

Can you show the example inputs and an output?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 12:52 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Here is a classical implementation:

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:
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 @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Count Apple2 JavaServer Pages (JSP) and JSTL 1 04-29-2008 11:02 AM
How to get the count of all the lines in a file Java Tip java.io 0 04-06-2008 09:45 PM
Getting row count Java Tip Java Tips 0 02-11-2008 10:49 AM
how to count 2 inserts together? kim85 New To Java 0 01-20-2008 01:25 PM
Getting row count from executeQuery() Java Tip Java Tips 0 12-05-2007 04:31 PM


All times are GMT +3. The time now is 08:38 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org