Question

I am trying to find the most number of consonants in a row for a specific word in an array. I have a while loop in the middle that I have commented, but for some reason it does not execute. Does anyone know why? Also you might notice that I have an int spaces because I am trying to find a sub array of String[] words. How might I do that as well?

import java.io.*;
import java.util.*
import java.util.Arrays;

public class Main
{
  public static void main (String[] args) 
  {
    System.out.print("Please enter a phrase to translate: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.nextLine();  
    String[] words = str.split("\\s+");
    int spaces = (word.length - 1);
    for (int i = 0; i < words.length; i++)
    {
        String a = words[i].substring(0,1);
        int k = a.length();
        int n = words[i].length();
        if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
        {
            while (k < 5) // start while  
            {
                if (n > k)
                {
                    a = words[i].substring(0,k);
                    k = k + 1;
                    }
                }
            } // end while
        if (words[i].startsWith("a") || words[i].startsWith("e") || words[i].startsWith("i") || words[i].startsWith("o") || words[i].startsWith("u"))
        {
            System.out.print(words[i] + "way");
            }
        else if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
        {
            String answer = words[i].substring(k,n);
            System.out.print(answer + a + "ay");
            }
        }
    }
}
Was it helpful?

Solution

Given your description of "trying to find the most number of consonants in a row for a specific word in an array", I would think something along these lines (not compiled or tested - just to show the approach) would be a lot simpler to follow/debug/test/maintain (also only looks at the current word[i] - an outer loop to iterate over your words is still needed:

String consgroups[] = word[i].split("[aeiou]"); // split on all vowels
int maxlen = consgroups[0].length, maxidx = 0;
for (int j = ; j < consgroups.length; ++j)    // iterate over groups and find max
  if (consgroups[j].length > maxlen)
  { maxidx = j;
    maxlen = consgroups[i].length;
  }

Given your comment about pig latin, I'm not sure why consonant groups other than at the beginning of the word are important, but perhaps I'm misunderstanding your description...

Edit: Now that I've looked a little harder, I think this approach might be more useful:

String vowels = "aeiou";
int i = 0;
while ((i < word.length) && (!vowels.contains(word.subSequence(i, i + 1))))
  ++i;
// now word[i] is the first non-vowel character in word
if (i == word.length) // word was all consonants
{ ...
}
else
  if (i) // word has a consonant prefix
  { ...
  }
  else // word starts with a vowel
  { ...
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top