Ok so I thought I figured it out but step 1 is still giving me some trouble. I have tested step one in its own program and it works fine but when I put it into the rest of the code it does not do what it is suppose to.
If I input the word "thinning" I should be getting this output:
After Rule 1: DHAMMAMC
After Rule 2: DAMMAMC
After Rule 3: DAMAMC
Result: DMMC
Would you like to perform another translation? y/n
But instead I am getting this output:
After Rule 1: THINNING
After Rule 2: TINNING
After Rule 3: TINING
Result: TINING
Would you like to perform another translation? y/n
Here is my code:
--------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class PlanitReduction {
/**
* Checks if the char identified by "currentChar" is a consonant.
* @param currentChar the char we are testing. Assumed to be UPPERCASE.
* @return true if currentChar is a consonant, false otherwise.
*/
private static boolean isConsonant(char currentChar){
if((currentChar == 'A') ||
(currentChar == 'E') ||
(currentChar == 'I') ||
(currentChar == 'O') ||
(currentChar == 'U')){
return false;
}
return true;
}
public static void main(String[] args) {
String answer = "y";
Scanner scan = new Scanner(System.in);
do{
String original = "";
String encoded = "";
//Getting the original message
System.out.println("Please enter your original word: ");
original = scan.next();
//Getting the whole word in upper case
original = original.toUpperCase();
//Step 1: Replace each character with it's appropriate letter:
String coded1 = original.replace('a', 'a')
.replace('e', 'a')
.replace('i', 'a')
.replace('o', 'a')
.replace('u', 'a')
.replace('y', 'a');
String coded2 = coded1.replace('b', 'b')
.replace('f', 'b')
.replace('p', 'b')
.replace('v', 'b');
String coded3 = coded2.replace('c', 'c')
.replace('g', 'c')
.replace('k', 'c')
.replace('j', 'c')
.replace('q', 'c')
.replace('s', 'c')
.replace('x', 'c')
.replace('z', 'c');
String coded4 = coded3.replace('d', 'd')
.replace('t', 'd');
String coded5 = coded4.replace('h', 'h')
.replace('w', 'h');
String coded6 = coded5.replace('l', 'l');
String coded7 = coded6.replace('m', 'm')
.replace('n', 'm');
encoded = coded7.replace('r', 'r');
System.out.println("After Rule 1: " + encoded);
//Step 2: Removes all H's if their occur after a consonant
for(int i = 1; i < encoded.length(); i++){
char currentChar = encoded.charAt(i);
if(currentChar == 'H'){
char previousChar = encoded.charAt(i-1);
if(isConsonant(previousChar)){
StringBuilder sb = new StringBuilder(encoded);
sb.deleteCharAt(i);
encoded = sb.toString();
}
}
}
System.out.println("After Rule 2: " + encoded);
//Step 3: Getting rid of repeated consecutive consonants
char previousChar = encoded.charAt(0);
for(int i = 1; i < encoded.length(); i++)
{
char currentChar = encoded.charAt(i);
if(isConsonant(currentChar) && (currentChar == previousChar))
{
StringBuilder sb = new StringBuilder(encoded);
sb.deleteCharAt(i);
encoded = sb.toString();
i--;
}
previousChar = currentChar;
}
System.out.println("After Rule 3: " + encoded);
// Step 4: Eliminating all A's
for(int i = 0; i < encoded.length(); i++)
{
char currentChar = encoded.charAt(i);
if(currentChar == 'A'){
StringBuilder sb = new StringBuilder(encoded);
sb.deleteCharAt(i);
encoded = sb.toString();
i--;
}
}
System.out.println("Result: " + encoded);
System.out.println("Would you like to perform another translation? y/n");
answer = scan.next();
}while(!answer.equals("n"));
System.out.println("Thank you!");
scan.close();
}
}