1

So I have a String Array (sConsonantArray) and have all of the consonants stored in it.

String[] sConsonantArray = new String[] {"q","w","r","t","p","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"};

I need to check if the second last value of a word (sWord) equals a value in the array and I don't know how to call each value in the array to compare the letters other than doing sConsonantArray[5] (checking them each one at a time). I am looking for an easier way to call them, thanks for your help. Also, it doesn't appear that the (&&) operator will work, other suggestions would be appreciated.

else if (sWord.substring(sWord.length()-2,sWord.length()-1).equals(sConsonantArray I DONT KNOW WHAT TO PUT HERE)) && (sWord.substring(sWord.length()-1,sWord.length()).equalsIgnoreCase("o"))

{
    System.out.println("The plural of " + sWord + " is " + (sWord + "es"));
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • You're missing a letter. I only count 20. There are 21 consonants. – Paul Samsotha Dec 24 '13 at 18:43
  • 1
    *"I need help,.."* Well duh! That seems a particularly inane thing to add to the title of a post to a Q&A site, and *"..thanks"* That is just noise. Leave them out of questtions in future. – Andrew Thompson Dec 24 '13 at 18:45
  • Unless your goal is educational, may be this link will help you with ready to use Java libraries for pluralization: http://stackoverflow.com/questions/5907296/plural-form-of-a-word – Max Yakimets Dec 26 '13 at 10:10

2 Answers2

3

It seems to me that it would be simpler to have the consonants as a string and then use charAt:

private static final String CONSONANTS = "bcdfgh...z";

if (CONSONANTS.indexOf(word.charAt(word.length() - 2)) {
    ...
}

If you really want to use an array, you could change your array to be in order and then call Arrays.binarySearch. Another alternative would be to create a HashSet<String> of the consonants and use contains on that.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Try something like

else if (Arrays.asList(sConsonantArray).contains(
     sWord.substring(sWord.length()-2,sWord.length()-1)) 
         &&  (sWord.substring(sWord.length()-1,sWord.length()).equalsIgnoreCase("o"))) {

        // do something
  }

or Write a small Util method

public static boolean isInConstants(String yourString){
String[] sConsonantArray = new String[] {"q","w...}
for (String item : sConsonantArray) {
    if (yourString.equalsIgnoreCase(item)) {
        return true;

    } 
}
 return false;

}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307