473,378 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

using indexOf to count the number of times a letter occurrs?

evilmonkey
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6.  
  7.  public static void main(String[] args) {
  8.  
  9.  
  10.   System.out.println("Enter the sentence :");
  11.  
  12.   alphaCount alpha =new alphaCount(); 
  13.   Scanner input = new Scanner(System.in);
  14.   String output = "";
  15.   String sentence = input.nextLine();
  16.   StringTokenizer token = new StringTokenizer (sentence );
  17.  
  18.   //String[] words = sentence.split(" "); 
  19.   System.out.println("Number of A's is :"+sentence.indexOf('a'));
  20.   //System.out.println("Number of B's is :"+alpha.numOfBs(sentence));
  21.  }
  22.  
  23. }
  24.  
  25. public class alphaCount {
  26.  
  27.  
  28.     /** Creates a new instance of alphaCount */
  29.     public alphaCount() {
  30.     }
  31.  
  32.   // I could brute force each letter of the alphabet like this 
  33.  public static int numOfAs(String str) {
  34.  int numA = 0;
  35.  
  36.   for(int i = 0;i < str.length();i++) {
  37.    if(str.charAt(i) == 'a') {
  38.     numA++;
  39.    }
  40.  
  41.   }
  42.   return numA;
  43.  }
  44.  
  45.  public static int numOfBs(String str) {
  46.   int numB =0;
  47.  
  48.   for(int i = 0;i < str.length();i++) {
  49.    if(str.charAt(i) == 'b') {
  50.     numB++;
  51.    }
  52.  
  53.   }
  54.   return numB;
  55.  }
  56.  
  57.  
  58.     }
Feb 21 '07 #1
4 13857
Ganon11
3,652 Expert 2GB
You could define a method, numOfChars, that takes a character as its parameter and returns the amount of that character in the string. Thus, you could use sentence.numOfChars('a'); to find the number of a's, sentence.numOfChars('G'); for G's, and so on.
Feb 21 '07 #2
r035198x
13,262 8TB
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8.  
  9.  
  10. System.out.println("Enter the sentence :");
  11.  
  12. alphaCount alpha =new alphaCount(); 
  13. Scanner input = new Scanner(System.in);
  14. String output = "";
  15. String sentence = input.nextLine();
  16. StringTokenizer token = new StringTokenizer (sentence );
  17.  
  18. //String[] words = sentence.split(" "); 
  19. System.out.println("Number of A's is :"+sentence.indexOf('a'));
  20. //System.out.println("Number of B's is :"+alpha.numOfBs(sentence));
  21. }
  22.  
  23. }
  24.  
  25. public class alphaCount {
  26.  
  27.  
  28. /** Creates a new instance of alphaCount */
  29. public alphaCount() {
  30. }
  31.  
  32. // I could brute force each letter of the alphabet like this 
  33. public static int numOfAs(String str) {
  34. int numA = 0;
  35.  
  36. for(int i = 0;i < str.length();i++) {
  37. if(str.charAt(i) == 'a') {
  38.     numA++;
  39. }
  40.  
  41. }
  42. return numA;
  43. }
  44.  
  45. public static int numOfBs(String str) {
  46. int numB =0;
  47.  
  48. for(int i = 0;i < str.length();i++) {
  49. if(str.charAt(i) == 'b') {
  50.     numB++;
  51. }
  52.  
  53. }
  54. return numB;
  55. }
  56.  
  57.  
  58. }
Might want to have a read at regular expressions as well.
Feb 22 '07 #3
bloid
1
Did you mean something like:
Expand|Select|Wrap|Line Numbers
  1. String str = "Hello there!!" ;
  2. char ch = 'e' ;
  3. int count = 0 ;
  4. int pos = -1 ;
  5. while( ( pos = str.indexOf( ch, pos + 1 ) ) > -1 ) count++ ;
  6. System.out.println( "The character " + ch + " occurs " + count + " times" ) ;
?
Feb 22 '07 #4
Did you mean something like:
Expand|Select|Wrap|Line Numbers
  1. String str = "Hello there!!" ;
  2. char ch = 'e' ;
  3. int count = 0 ;
  4. int pos = -1 ;
  5. while( ( pos = str.indexOf( ch, pos + 1 ) ) > -1 ) count++ ;
  6. System.out.println( "The character " + ch + " occurs " + count + " times" ) ;
?
Yes I did, but I actually figured it out and it is working, that was close to what I got.
thank you for the reply
Feb 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
4
by: onecorp | last post by:
I have a SQL table comprised of 31 columns. The first column is simply an id column, the next 30 columns are labelled ,.... The numerical columns have a tinyint type and the data stored is either...
5
by: isabelle | last post by:
hi, every body.. I have two program I couldn’t solve them So, can any body help me. please!! 1-Write a program that accepts a character and count number of occurrences in a file. The file...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.