Question Write a program that reads lines of input from the user

Question

Write a program that reads lines of input from the user and converts each line into “Pig Latin.” Pig Latin is English with the initial consonant sound moved to the end of each word, followed by “ay”. Words that begin with vowels simply have an “ay” appended.

Your program should continuously prompt the user for sentences until they enter a blank line, at which point the program should terminate.

SAMPLE RUN #1: java PigLatin

Enter·a·sentence·to·translate:It·was·a·bright·cold·day·in·April·and·the·clocks·were·striking·thirteen↵

It-ay·as-way·a-ay·ight-bray·old-cay·ay-day·in-ay·April-ay·and-ay·e-thay·ocks-clay·ere-way·riking-stay·irteen-thay·↵

Enter·a·sentence·to·translate:It·was·the·best·of·times·it·was·the·worst·of·times↵

It-ay·as-way·e-thay·est-bay·of-ay·imes-tay·it-ay·as-way·e-thay·orst-way·of-ay·imes-tay·↵

Enter·a·sentence·to·translate:If·you·really·want·to·hear·about·it·the·first·thing·you’ll·want·to·know·is·where·I·was·born↵

If-ay·ou-yay·eally-ray·ant-way·o-tay·ear-hay·about-ay·it-ay·e-thay·irst-fay·ing-thay·ou’ll-yay·ant-way·o-tay·ow-knay·is-ay·ere-whay·I-ay·as-way·orn-bay·↵

Enter·a·sentence·to·translate:↵

My answer:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class PigLatin {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str;

while (true) {

// read the input infinitely

System.out.println(“Enter·a·sentence·to·translate:”);

str = br.readLine();

// terminate the program when blank string is entered

if (str.isBlank() || str.isEmpty())

break;

StringBuffer solution = new StringBuffer();

String[] strArr = str.split(” “);

// extract each word fromm the sentence

for (String word : strArr) {

char ch = word.charAt(0);

int index = getIndexOfVowel(word);

if (index == 0) {

solution.append(word).append(“-ay”).append(” “);

} else {

// Divide each word into two parts , one before vowel and another after vowel.

// Finally append those two parts to form the solution , after appending “ay” to

// it.

String start = word.substring(index);

String end = word.substring(0, index);

solution.append(start).append(“-“).append(end).append(“ay”).append(” “);

}

}

System.out.println(solution.toString());

}

}

/**

* Retruns the index of the first vowel in the word.

*

* @param word

*

*/

private static int getIndexOfVowel(String word) {

for (int i = 0; i < word.length(); i++) {

char ch = word.charAt(i);

if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ || ch == ‘A’ || ch == ‘E’ || ch == ‘I’

|| ch == ‘O’ || ch == ‘U’)

return i;

}

return -1;

}

}

Results :

Failed Test Run #1

     ⇒     The contents of your standard output is incorrect.
     ⇒     There is an error in your prompts.

Hide Invisibles

Highlight: Show Highlighted Only 

Expected Result:

Enter·a·sentence·to·translate:

It·was·a·bright·cold·day·in·April·and·the·clocks·were·striking·thirteen↵

It-ay·as-way·a-ay·ight-bray·old-cay·ay-day·in-ay·April-ay·and-ay·e-thay·ocks-clay·ere-way·riking-stay·irteen-thay·↵

Enter·a·sentence·to·translate:

It·was·the·best·of·times·it·was·the·worst·of·times↵

It-ay·as-way·e-thay·est-bay·of-ay·imes-tay·it-ay·as-way·e-thay·orst-way·of-ay·imes-tay·↵

Enter·a·sentence·to·translate:

If·you·really·want·to·hear·about·it·the·first·thing·you’ll·want·to·know·is·where·I·was·born↵

If-ay·ou-yay·eally-ray·ant-way·o-tay·ear-hay·about-ay·it-ay·e-thay·irst-fay·ing-thay·ou’ll-yay·ant-way·o-tay·ow-knay·is-ay·ere-whay·I-ay·as-way·orn-bay·↵

Enter·a·sentence·to·translate:

Your Code’s Actual Result:

Enter?a?sentence?to?translate:

It·was·a·bright·cold·day·in·April·and·the·clocks·were·striking·thirteen↵

It-ay·as-way·a-ay·ight-bray·old-cay·ay-day·in-ay·April-ay·and-ay·e-thay·ocks-clay·ere-way·iking-stray·irteen-thay·↵

Enter?a?sentence?to?translate:

It·was·the·best·of·times·it·was·the·worst·of·times↵

It-ay·as-way·e-thay·est-bay·of-ay·imes-tay·it-ay·as-way·e-thay·orst-way·of-ay·imes-tay·↵

Enter?a?sentence?to?translate:

If·you·really·want·to·hear·about·it·the·first·thing·you’ll·want·to·know·is·where·I·was·born↵

If-ay·ou-yay·eally-ray·ant-way·o-tay·ear-hay·about-ay·it-ay·e-thay·irst-fay·ing-thay·ou’ll-yay·ant-way·o-tay·ow-knay·is-ay·ere-whay·I-ay·as-way·orn-bay·↵

Enter?a?sentence?to?translate: