SeidlerAM SeidlerAM - 29 days ago 8
Java Question

Piglatin, difficulty understanding how to move a '.' in a String

I am having great difficulty with this program I am writing for my computer science class. I've gone through and looked at other programs that students have posted, here and on other sites, but I haven't been been able to understand what I am doing wrong. I have looked at the Java oracle doc's and it's just not clicking with me.

My main problem is the nested section if statement that should move the '.' to the end of the sentence after adding "ay" to the word. The application isn't complete. After I can learn how to solve this step I'll move on to capitalization of the first word in the sentence. I just don't understand why I am having difficulty with .substring and .charAt.

Please, explain the answer.

File input = new File ("elab.txt");
File output = new File ("elab.out");

//create Scanner
Scanner in = new Scanner(input);

//create writer
PrintWriter out = new PrintWriter(output);

//VariParty, Part 1
int count = 0;//Simple counter
String word;//Scanned word from text file, will be edited
String original;//Original word, backed up from String "word" before edit
String original1;//Used in consonants
String sentence = "";//Declare full sentence

/* What I will do is scan each word in the text document. The application will display
* a number for each word for my visual learning pleasure. The counter will be //commented
* out as to not cause any visual action for the complete application.
*/


while(in.hasNext()){//Star while
word = in.next();//Scans individual word
word = word.toLowerCase();
count++;//adds to counter
original = word.toLowerCase();
original1 = word.toLowerCase();

//VariParty, Part 2
char fl = word.charAt(0);
char fL = Character.toLowerCase(word.charAt(0));
char endChar = word.charAt(word.length()-1);


/* This vowel portion of the application will check each "word" and deduce if "ay"
* should be added to the end of the word beginning with a vowel. Finally, the code
* will build onto the sentence string to produce a sentence.
*/

if (fl == 'a' || fl == 'e' || fl == 'i'|| fl == 'o' || fl == 'u'){//Start if
word = word + "ay";
System.out.println(count + " VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");
sentence = sentence + " " + word;
}//End if


/* We will now work towards building a better sentence. Our sentence will not have the
* audacity to place a period in the middle of the word! Our sentences shall forever be
* properly defined with a punctuation mark at the end, unless it's an apostrophe.
*/

if(word.contains(".")){//Start nested if
word = word.substring(0,word.length()-1);
word = word + ".";
System.out.println(count + " NES VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
}//End nested if


/* Now we will move the word around and deal with the consonants
* The else statement will build the proper word by moving the first
* letter to the end of the sentence, then adding "ay" to the end.
* The final line will be building onto the sentence string.
*/


else{//Start else
word = word.substring(1,word.length());
original = word;
word = word + fl + "ay";
System.out.println(count + " CON) " + "\"" + original1 + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");

sentence = sentence + " " + word;

}//End else
}//End While

System.out.println(sentence);
in.close();//Close Scanner
out.close();//Close PrintWriter

Answer

The first thing you should do is to make your program testable.

The main functionality is converting a word into piglatin. Therefore you should define a method that does exactly that:

public static String translateWordToPigLatin(String word) {
    ....
    return ...;
}

Then, you should write another class called PigLatinTest, in which you test this method:

public class PigLatinTest {

    public static void main(String[] args) {
        testPigLatin("hello", "ellohay");
        testPigLatin("welcome", "ellcomeway");
        // Add more test cases here.
    }

    private static void testPigLatin(String english, String expectedPigLatin) {
        String actual = translateWordToPigLatin(english);
        if (!expectedPigLatin.equals(actual)) {
            throw new AssertionError("Expected \"" + expectedPigLatin + "\" for \"" + english + "\", got \"" + actual "\".");
        }
    }
}

In the translateWordToPigLatin method, I called the parameter word. This is a strong indication that a '.' doesn't belong there. So what you should really do is to extract all proper words from the line, where a word consists only of letters.

The next bigger method should be called translateLineToPigLatin(String line), and in this method, you should:

  1. Start at the beginning of the English line.
  2. Copy the characters to the pig latin line until you find a letter.
  3. Scan further in the English line until you find no letter anymore.
  4. The text between step 2 and step 3 is a word.
  5. Convert that word to pig latin.
  6. Add the converted word to the pig latin line.
  7. Continue scanning the English line where you left off.