Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: A java problem for you

  1. #1
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default A java problem for you

    Hello,
    I wanted to see how others would have solved the following problem.

    Create an array of 5 Strings containing people's names. Write a program that counts and displays the total number of vowels in the array.

    Here's how I did it.

    Java Code:
    public class Vowels{
       public static void main(String[] args){
    	   String[] names = {"Rob", "Tom", "Dad", "Mom", "Sharon"};
    		int vowelCount = 0;
    		for(int i = 0; i < names.length; i++){
    		   for(int j = 0; j < names[i].length(); j++){ 
    			   if(names[i].substring(j,j+1).equals("a") ||
    				   names[i].substring(j,j+1).equals("e") ||
    					names[i].substring(j,j+1).equals("i") ||
    					names[i].substring(j,j+1).equals("o") ||
    					names[i].substring(j,j+1).equals("u"))
    					vowelCount = vowelCount + 1;
    			   /*System.out.println(names[i].substring(j,j+1));*/
    			}
    		}
    		System.out.println(vowelCount);
    	
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: A java problem for you

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    pbrockway2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A java problem for you

    I'd use charAt() and the expressive idiom "whatever".indexOf(someChar). Both are String methods.

    I'd also ask for clarification from whoever posed the problem concerning what to do with aunty Yvonne, cousin Jenny et al. (Although Al isn't really controversial.)
    Last edited by pbrockway2; 06-25-2012 at 02:55 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: A java problem for you

    You do this 6 times
    names[i].substring(j,j+1)
    vs one time and use the results.

    Indexing and substring are expensive and the repeated use of expressions leaves lots of room for coding errors.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: A java problem for you

    How about this:

    Java Code:
    
    import java.util.Arrays;
    
     
    public class JavaApplication23 {
     public static void main(String[] args){
           String[] names = {"Rob", "Tom", "Dad", "Mom", "Sharon"};
           int x=Arrays.toString(names).replaceAll("[^aAeEiIoOuU]","").length();
            
            System.out.println(x);
         
        }
    }
    Ksharp

Similar Threads

  1. Replies: 4
    Last Post: 03-16-2012, 12:29 PM
  2. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 04:43 AM
  3. Help! Java Problem!!
    By TheBreakdown in forum New To Java
    Replies: 2
    Last Post: 08-04-2011, 10:06 AM
  4. Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
    By Ravanelly in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 10:36 AM
  5. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •