3 Replies - 5954 Views - Last Post: 20 February 2007 - 05:35 PM Rate Topic: -----

#1 evilMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-February 07

Using indexOf to determine the total number of each letter.

Posted 20 February 2007 - 01:28 PM

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 February 2007 - 01:28 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Using indexOf to determine the total number of each letter.

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Using indexOf to determine the total number of each letter.

Posted 20 February 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.
Was This Post Helpful? 0
  • +
  • -

#3 evilMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-February 07

Re: Using indexOf to determine the total number of each letter.

Posted 20 February 2007 - 03:04 PM

View Postjayman9, on 20 Feb, 2007 - 02:14 PM, said:

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"

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.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Using indexOf to determine the total number of each letter.

Posted 20 February 2007 - 05:35 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1