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
73.6k35 gold badges178 silver badges240 bronze badges
asked Dec 24, 2013 at 18:32
3
  • You're missing a letter. I only count 20. There are 21 consonants. Commented Dec 24, 2013 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. Commented Dec 24, 2013 at 18:45
  • Unless your goal is educational, may be this link will help you with ready to use Java libraries for pluralization: stackoverflow.com/questions/5907296/plural-form-of-a-word Commented Dec 26, 2013 at 10:10

2 Answers 2

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.

answered Dec 24, 2013 at 18:38
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;
}
answered Dec 24, 2013 at 18:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.