Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Combining arrays and counting vowels in String arrays

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Combining arrays and counting vowels in String arrays

    Hi!
    Thank you for helping, and I apologize if this is the wrong thread. I hope I'm posting in the right place.

    What I want to do is take two arrays and combine them.
    String[] word = {"happy", "live", "walk"};
    String[] word2 = {"joy", "full", "forward"};

    What I want it to look like is this:
    String[] combo = {"happy joy", "live full", "walk forward"};

    I am very beginner and am only now taking intro course in school. Here is what I have but it's printing null before each new word and I don't know why. I don't think we learned the part about Arrays.toString for it to print properly but I found that in searching online and don't know if there's some other way to do it for beginner.

    Here's what I did do:
    int len = word.length;
    String[] res = new String[len];
           int pos = 0;
     
           while(pos < len)
           {
               String nWord = word[pos] + " " + word2[pos];
               res[pos] += nWord;
               pos++;
           }
     
           System.out.println(Arrays.toString(res));


    I am also trying to figure out how to count vowels in a string array so I can find the word with the least vowels. If two or more words have the fewest, I want the smaller of the two words.
    If I have,
    String[] wordList = {"hello", "hey", "expression", "both"};
    How do I go about getting something back to say "hey"?

    I have this, so far:
    public static int countVowels(String s)
    	{
    		 int count = 0;        
            for(int i = 0; i < arr.length; i++)
            {
                if(isVowel(arr.charAt(i)))
                    count++;
            }
            return count;
    	}
    	public static boolean isVowel(char ch)
    	{
    		return ch == 'a' ||
    			   ch == 'e' ||
    			   ch == 'i' ||
    			   ch == 'o' ||
    			   ch == 'u';
    	}

    If it helps, I've been using CSAwesome and got up to chapter 6
    Last edited by F3rEgl; December 15th, 2021 at 01:31 AM. Reason: adjusted code

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    but it's printing null before each new word
    The contents of the res array is initially null. This statement concatenates the new words to its current contents:
             res[pos] += nWord;  //  adds new word to end of existing value
    Change the code to assign the value of the new word to the res element by using = vs +=
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    Thank you so much, that did it!

    Are you (or is anyone) able to help with the counting vowels one, please? I'm so stumped. Any hints on how I do this would be so great. I just have no idea. Would it be similar to how I did the combine words? Would I somehow have to input each word to a new string and then write something so it looks for a vowel in that word?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    help with the counting vowels
    What does the code do now?
    How do you want to change what it does?
    What is the desired output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    Quote Originally Posted by Norm View Post
    What does the code do now?
    How do you want to change what it does?
    What is the desired output?


    I am trying to figure out how to count vowels in a string array so I can find the word with the least vowels. If two or more words have the fewest, I want the smaller of the two words.
    If I have,
    String[] wordList = {"hello", "hey", "expression", "both"};
    How do I go about getting something back to say "hey"?

    I have very little, so far because I don't know what to put in the method for minVowels. I don't understand how to use countVowels to help with minVowels.
    I thought minVowels only tells me if the word has a vowel, I don't know understand if countVowels actually counts the number of the vowels in each word. All in all, I think I'm confused by this one altogether.

    I'm a beginner and have only finished up to chapter 6 in CSAwesome (if that helps at all).

    public static void main(String[] args)
       { 
    String[] arr = {"hello", "hey", "expression", "both"};
    System.out.println(minVowels(arr));		
    	}
    	public static String minVowels(String[] words)
    	{     
    	}
     
    	public static int countVowels(String s)
    	{
            int count = 0;        
            for(int i = 0; i < s.length(); i++)
            {
                if(isVowel(s.charAt(i)))
                    count++;
            }
            return count;
     
    	}
    	public static boolean isVowel(char ch)
    	{
    		return ch == 'a' ||
    			   ch == 'e' ||
    			   ch == 'i' ||
    			   ch == 'o' ||
    			   ch == 'u';
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    find the word with the least vowels.
    Ok, work out the logic for that.
    Compare two words, save the smallest. Look at next word and compare its length against the smallest so far, save the smallest. Continue until at end of list of words. By then you will have saved the smallest.

    Use a loop to work through the array of words. One way is the foreach loop, another way is to do the indexing yourself.

    for(String word : arrayOfWords)
    // here word has next word from array

    for(int i=0; i < arrayOfWords.length; i++)
    String word = arrayOfWords[i]; // get next word
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    Quote Originally Posted by Norm View Post
    Ok, work out the logic for that.
    Compare two words, save the smallest. Look at next word and compare its length against the smallest so far, save the smallest. Continue until at end of list of words. By then you will have saved the smallest.

    Use a loop to work through the array of words. One way is the foreach loop, another way is to do the indexing yourself.

    for(String word : arrayOfWords)
    // here word has next word from array

    for(int i=0; i < arrayOfWords.length; i++)
    String word = arrayOfWords[i]; // get next word

    Sorry, I'm not understanding this. Here's what I'm getting, I think, at least.

    I need to loop through the array of words. A for each loop will do that cause it stops at each word, right?

    So if I do:
    for(String word : words)

    that will get me to start with looking at the first word in the initial string (String[] arr) which is "hey"
    From there, though, how do I look at each letter in hey using isVowels and countVowels?

    I'm thinking since I used isVowels in countVowels, then I just use countVowels, right?

    so would I use an if statement like I was doing below -- countVowels(word)?

    I don't know if I'm even on the right track below, but that's what I was thinking maybe... the issue I'm having is I need a return statement and don't know how to get that so it gives me the word back. Maybe I'm overcomplicating it?



    public static void main(String[] args)
       { 
            String[] arr = {"hey","yolo","hi","this is long"};
            System.out.println(minVowels(arr));
    		//should print "hi"
    	}
    	public static String minVowels(String[] words)
    	{
            for(String word : words)
            {
                for(int i = 0; i < word.length(); i++)
                {
                    String[] track = new String[word.length()];
                    word = words[i];
                    if(countVowels(track[i]) > countVowels(track[i+1]))                
                    {
                        track[i] = track[i+1];
     
                    }
                }             
            }             
    	}
     
    	public static int countVowels(String s)
    	{
            int count = 0;        
            for(int i = 0; i < s.length(); i++)
            {
                if(isVowel(s.charAt(i)))
                    count++;
            }
            return count;
     
    	}
    	public static boolean isVowel(char ch)
    	{
    		return ch == 'a' ||
    			   ch == 'e' ||
    			   ch == 'i' ||
    			   ch == 'o' ||
    			   ch == 'u';
            }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    Design the program before trying to write code for it.
    Explain what steps need to be taken to solve the problem.
    First step would be to have a loop that looks at each word one at a time
    Then what?

    When you get a good list of the steps needed, then work on writing code for those steps.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    Quote Originally Posted by Norm View Post
    Design the program before trying to write code for it.
    Explain what steps need to be taken to solve the problem.
    First step would be to have a loop that looks at each word one at a time
    Then what?

    When you get a good list of the steps needed, then work on writing code for those steps.
    Ok,
    First step, like you said, a loop to look through each word one at a time
    Second I need to count the vowels in each word
    Third I need to compare the vowel count in each word
    Fourth I need to return the word with the fewest vowels; if two words have this spot, I need the word that is shortest in length



    So the first step would be a loop, like the for each loop
    The second, an int to count the vowels
    Third, if statements - if the vowel count of the first word is the same as the second word, then compare word.length for each and keep the shorter word. Otherwise, if vowel count of the first word is less than the second; keep the first one. Maybe this is a loop, too?
    Fourth, return the word with fewest vowels

    So, would I maybe make an array of integers to hold the vowel counts? Then make another for each loop and compare each number that way?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combining arrays and counting vowels in String arrays

    make an array of integers to hold the vowel counts
    Do you need to remember what the counts were? Can the count be tested as each word is processed and the smallest value saved at that time?

    Otherwise the logic looks reasonable. The Third step needs some clarification. A problem with a search for the largest or smallest value in a list of values is how to compare the first two without caring that they are the first pair being tested vs any other comparison.
    One way is to assume the first item is the largest/smallest and save its value. Then for the rest use a loop that compares each against the saved value and save the new one if it fits the criteria.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Counting Vowels and Consonants in a String.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 8th, 2013, 09:59 PM
  2. Counting element(s) appearance(s) in arrays
    By alan55 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 20th, 2012, 01:03 AM
  3. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  4. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  5. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM