Welcome to Dream.In.Code
Become a Java Expert!

Join 149,823 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,626 people online right now. Registration is fast and FREE... Join Now!




Using indexOf to determine the total number of each letter.

 
Reply to this topicStart new topic

Using indexOf to determine the total number of each letter.

evilMonkey
20 Feb, 2007 - 12:28 PM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 3


My Contributions
Does anyone know if there is a way to use the String method indexOf to return the number of occurrences of each letter of the alphabet?

This post has been edited by evilMonkey: 20 Feb, 2007 - 12:28 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Using IndexOf To Determine The Total Number Of Each Letter.
20 Feb, 2007 - 01:14 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,313



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You need to loop through the string using the indexOf method to COMPARE to the character you are searching for. You can use the length method to determine how many times to iterate through the loop.

You will need an IF statement inside the loop to do the comparison.

Post the code you have completed and we will be happy to help you get it working.
User is online!Profile CardPM
+Quote Post

evilMonkey
RE: Using IndexOf To Determine The Total Number Of Each Letter.
20 Feb, 2007 - 02:04 PM
Post #3

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 3


My Contributions
QUOTE(jayman9 @ 20 Feb, 2007 - 02:14 PM) *

You need to loop through the string using the indexOf method to COMPARE to the character you are searching for. You can use the length method to determine how many times to iterate through the loop.

You will need an IF statement inside the loop to do the comparison.

Post the code you have completed and we will be happy to help you get it working.



Here is what I have, I am very new to programming and Java So I am not sure if I am heading in the right direction. I am just starting off by tring to check for "A's"

CODE

import java.util.StringTokenizer;
import java.util.Scanner;



public class Main
{
  
   public static void main (String[] args)
    {
    
    String phrase;
    String oneMore;

    Scanner input = new Scanner(System.in);

    do
    {
    System.out.println();
    System.out.println();
    System.out.println ("Enter a phrase please:");
    
    phrase = input.nextLine();

    int length = phrase.length();
    int countA = 0;
    int index = 0;

    while (index < length) {
    if (phrase.indexOf() == 'a' ) {
    countA = countA + 1;
    }
    index = index + 1;

    }
    System.out.println ("Number of A's" + countA);
//    System.out.println ("Number of B's" + countB);
//    System.out.println ("Number of C's" + countC);
//    System.out.println ("Number of D's" + countD);
//    System.out.println ("Number of E's" + countE);
//    System.out.println ("Number of F's" + countF);
//    System.out.println ("Number of G's" + countG);
//    System.out.println ("Number of H's" + countH);
//    System.out.println ("Number of I's" + countI);
//    System.out.println ("Number of J's" + countJ);
//    System.out.println ("Number of K's" + countK);
//    System.out.println ("Number of L's" + countL);
//    System.out.println ("Number of M's" + countM);
//    System.out.println ("Number of N's" + countN);
//    System.out.println ("Number of O's" + countO);
//    System.out.println ("Number of P's" + countP);
//    System.out.println ("Number of Q's" + countQ);
//    System.out.println ("Number of R's" + countR);
//    System.out.println ("Number of S's" + countS);
//    System.out.println ("Number of T's" + countT);
//    System.out.println ("Number of U's" + countU);
//    System.out.println ("Number of V's" + countV);
//    System.out.println ("Number of W's" + countW);
//    System.out.println ("Number of X's" + countX);
//    System.out.println ("Number of Y's" + countY);
//    System.out.println ("Number of Z's" + countZ);







    System.out.println("Would you like to check another phrase?");
    System.out.println("Enter y/n");
    System.out.println();
    oneMore = input.nextLine();    
    System.out.println();
  }
    while (oneMore.equalsIgnoreCase("y"));
}
}


PS I use netbeans and it for some reason it cant find the method index0f()
I am kinda frusterated.

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using IndexOf To Determine The Total Number Of Each Letter.
20 Feb, 2007 - 04:35 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
From the JAVA API:

String
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

So lets see, I am shooting from the hip here, find the first occurence

a=st.indexOf("a", 1)

If a>0 then an instance was found and you want to do the next search from a+1. Count up how many occurences you have.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:05AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month