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"));
}
-
You're missing a letter. I only count 20. There are 21 consonants.Paul Samsotha– Paul Samsotha2013年12月24日 18:43:50 +00:00Commented 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.Andrew Thompson– Andrew Thompson2013年12月24日 18:45:09 +00:00Commented 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-wordMax Yakimets– Max Yakimets2013年12月26日 10:10:17 +00:00Commented Dec 26, 2013 at 10:10
2 Answers 2
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.
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;
}