DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Positions of the letters in a word

  1. #1
    Join Date
    May 2006
    Posts
    4

    Positions of the letters in a word

    Hi, i'm doing this vowels program,
    and i want that the program also prints the position of a letter when i enter it by keyboard.

    thanks for any response.

    this is the program

    Code:
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    
      class Nwe{
      	
      	public static String readWord() {
       	String word = null;
       	BufferedReader reader = null;
       	try {
       		System.out.println("Enter:\n");
    	reader = new BufferedReader(new InputStreamReader(System.in));
    	word = reader.readLine();
       	} catch (IOException e) {
    	e.printStackTrace();
       	}
    	return word;
    	}
    	
    	
    	public static Vector loadVector() {
    	Vector words = new Vector();
    	String newWord = readWord();
    	while (newWord != null && !newWord.equalsIgnoreCase("stop")) {
    		words.add(newWord);
    		newWord = readWord();
    	}
    	return words;
    	}
    	
    	
    	
    	public static String[] toArray(Vector v){
    	//Load an array with the vector items
    	int numberOfWords = v.size();
    	String[] allWords = new String[numberOfWords];
    	v.toArray(allWords);
    	
    	return allWords;
    	}
    	
    	
    	public static int countLetter(String allWords, char a){
    				int count = 0;
    				int index = allWords.indexOf(a);
    				while(index > 0){
    				count++;
    				index = allWords.indexOf(a,++index);
    				}
    				return count;	
    	}
    	
    
    	public static void printVowels(String[] allWords){
     	for (int i = 0; i < allWords.length; i++) {
    	System.out.println("Vowels found in: " + allWords[i]);
    	System.out.println("a: " + countLetter(allWords[i], 'a'));
    	System.out.println("e: " + countLetter(allWords[i], 'e'));
    	System.out.println("i: " + countLetter(allWords[i], 'i'));
    	System.out.println("o: " + countLetter(allWords[i], 'o'));
    	System.out.println("u: " + countLetter(allWords[i], 'u'));	
       		}
    
    	}
    	
    
    	public static void main(String []args) throws Exception{
    		Nwe nw = new Nwe();
    		nw.readWord();
    		Vector v = loadVector();
    		String[] allWords = toArray(v);
    		nw.printVowels(allWords);
    		
    	
    	}
    	
      }
    Last edited by derok; 05-10-2006 at 12:00 AM.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Position of the letter in what? You have to give a little more information than just saying "the vowel program".

  3. #3
    Join Date
    May 2006
    Posts
    4
    for example, i enter a word like "apple", and then program asks the user to enter a letter to show its position in the word, something like: "a" is at position 0, "e" is at position 4.
    i already solved the problem, but thanks.

Similar Threads

  1. How to get the word pointed by mouse pointer
    By Berni HAN in forum VB Classic
    Replies: 0
    Last Post: 09-30-2002, 07:23 PM
  2. How to Get the word under the mouse pointer ?
    By Berni HAN in forum VB Classic
    Replies: 1
    Last Post: 09-26-2002, 03:23 PM
  3. Replies: 5
    Last Post: 02-22-2002, 12:01 PM
  4. How to determine the version of Word a Word Doc was saved in
    By Graham Russell in forum VB Classic
    Replies: 16
    Last Post: 01-04-2002, 10:14 PM
  5. How to determine the version of Word a Word Doc was saved in
    By Graham Russell in forum VB Classic
    Replies: 0
    Last Post: 01-03-2002, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center