Reputation: 363
Hi I am trying to write a program that separates words into syllables using romanian rules.Trying to do this is a lot harder then I thought because I don't know how I can create this rules.This is what I have so far and I know that the code is not correct at the condition but I need check if the letter in the car array exists in the vocale array then to check if the next letter in car array exists in the consoane array and so one here is the code I know the if condition is typed wrong I just need a solution to check that condition:
String[] vocale = {"a", "e", "i", "o", "u"};
String[] consoane = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
public String DesparteCuvant(String cuv){
String[] car = new String[cuv.length()];
String c = "";
for(int i = 0; i < cuv.length(); i++){
car[i] = cuv.substring(i, i+1);
}
for (int j = 0; j < car.length - 2; j++){
if(car[j] == vocale[] && car[j+1] == consoane[] && car[j+2] == vocale[]){
}
}
return c;
}
Upvotes: 2
Views: 1831
Reputation: 4732
I think you could use regular expressions for this task. They allow you to more easily write down such patterns.
As example
// following is the enough to specify a "complex" pattern
Pattern rule1 = Pattern.compile("([aeiou][bcdfghjklmnpqrstvwxyz][aeiou])");
Matcher matcher= rule1.matcher(strLine);
while (matcher.find()){
String aMatch= matcher.group(1);
// do what you need to do
}
would be a replacement for your
for (int j = 0; j < car.length - 2; j++){
if(car[j] == vocale[] && car[j+1] == consoane[] && car[j+2] == vocale[]){
}
}
Upvotes: 1
Reputation: 1167
Ok, if you just want to get the if statement working this is how i would do it:
private final Set<String> vocale = new HashSet<String>();
private final Set<String> consoane = new HashSet<String>();
private init(){
// fill the sets with your strings
}
private boolean isVocale(String s){
return vocale.contains( s );
}
private boolean isConsoane(String s){
return consoane.contains( s );
}
And your if statement would look like this:
if(isVocale(car[j]) && isConsoane( car[j+1] ) && isVocale( car[j+2] ) ){
// do your stuff
}
Upvotes: 1