Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String Problem For each numerical value 0, 1, 2, …9 (0 <= NUMBER <= 9), embedded

ID: 3902940 • Letter: S

Question

String Problem

For each numerical value 0, 1, 2, …9 (0 <= NUMBER <= 9), embedded in a sentence, convert that value to its equivalent English text. Print the converted sentence both to the screen and to an output file.

Your input file consists of a variable number of records. Each record is a sentence of length <= 80 characters. More than one numerical value in the given range may appear in a sentence. You must deal with upper and lower case issues. If a line begins with a single digit, write that digit as a word starting with an uppercase letter. See the examples below.

Examples:

Input Record:

          3 foxes were chasing 5 rabbits and 10 ducks.

Output Record:

         Three foxes were chasing five rabbits and 10 ducks.

Input Record:

         I used 3 eggs out of the 12 for the cake.
Output Record:

         I used three eggs out of the 12 for the cake.

Input Record:

         1 picture is worth 1000 words.

Output Record:

         One picture is worth 1000 words.

Input Record:

          There are 260 students enrolled in Java.
Output Record:

          There are 260 students enrolled in Java.

Create the following as an input file for testing.

          The 8 eggs were separated into 3 groups.

          5 boys and 7 girls were present.

          He was 1 hour and 5 minutes late.

          She ate 3 dozen doughnuts!

          4 dogs were chasing 3 cats.

          The captain said, “This is the 0 hour”.

          I tried to call you 9 times today; Ann tried 6 times!!

          The 12 firemen worked quickly.

Prompt the user for the name of the input file. Name your output file “outSentences.txt” .   Save the output file in the same directory as your code to make grading on different systems easier.

More details:

Create two class files. One class is the Converter. It has a StringBuilder field for the original sentence and a String field for the converted sentence.

The constructor will call a method to convert the original sentence.

You are to use only a select few methods of the StringBuilder class for the conversion. These are the only methods of the String and StringBuilder class you are permitted to use:

StringBuilder:                                      String:

constructor s                                         constructors
charAt( )                                             charAt(   )                                                
length( )
length( )
replace( )

You are also permitted the use of the isDigit( ) method from the Character class.

The second class is the driver class The driver will contain the main( ) method. The main( ) method will open a file for input. Read the file line by line and send each line, one line at a time, to the Converter class. The

main( ) method will call the get method( ) of the Converter class to get the converted string, and print this string both to the screen and to a file.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Converter.java

public class Converter {

                private StringBuilder originalText;

                private String convertedText;

                public Converter(StringBuilder text) {

                                originalText = text;

                                convert();//converting the text

                }

                /**

                * method to convert the text

                */

                public void convert() {

                                convertedText = "";

                                //looping through all characters in the original text

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

                                                //getting character at current position

                                                char c = originalText.charAt(i);

                                                //checking if c is a digit

                                                if (Character.isDigit(c)) {

                                                                //assuming this is a single digit

                                                                boolean isSingleDigit = true;

                                                                if ((i + 1) < originalText.length()) {

                                                                                if (Character.isDigit(originalText.charAt(i + 1))) {

                                                                                                //next character is a digit, so this is not a single digit

                                                                                                isSingleDigit = false;

                                                                                }

                                                                }

                                                                if ((i - 1) >= 0) {

                                                                                if (Character.isDigit(originalText.charAt(i - 1))) {

                                                                                                //previous character is a digit, so this is not a single digit

                                                                                                isSingleDigit = false;

                                                                                }

                                                                }

                                                                if (isSingleDigit) {

                                                                                //single digit, converting to corresponding text

                                                                                String digitText = digitToText(c);

                                                                                //checking if this is the first character

                                                                                if (convertedText == "") {

                                                                                                //converting first character of the text to upper case

                                                                                                digitText = digitText.replace(digitText.charAt(0),

                                                                                                                                (char) ('A' + (digitText.charAt(0) - 'a')));

                                                                                }

                                                                                //appending to converted text

                                                                                convertedText += digitText;

                                                                } else {

                                                                                //not a single digit appending unchanged character to converted text

                                                                                convertedText += c;

                                                                }

                                                }else{

                                                                //not a digit,appending unchanged character to converted text

                                                                convertedText += c;

                                                }

                                }

                }

                /**

                * a helper method to convert a digit to corresponding text

                * @param digit - 0-9

                * @return - text equivalent of digit in lowercase

                */

                private String digitToText(char digit) {

                                String res = "";

                                switch (digit) {

                                case '0':

                                                res = "zero";

                                                break;

                                case '1':

                                                res = "one";

                                                break;

                                case '2':

                                                res = "two";

                                                break;

                                case '3':

                                                res = "three";

                                                break;

                                case '4':

                                                res = "four";

                                                break;

                                case '5':

                                                res = "five";

                                                break;

                                case '6':

                                                res = "six";

                                                break;

                                case '7':

                                                res = "seven";

                                                break;

                                case '8':

                                                res = "eight";

                                                break;

                                case '9':

                                                res = "nine";

                                                break;

                                }

                                return res;

                }

                /**

                * getter method for the converted text

                */

                public String getConvertedText() {

                                return convertedText;

                }

}

// Driver.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Driver {

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

                                //Converter object

                                Converter converter;

                                Scanner scanner=new Scanner(System.in);

                                System.out.print("Enter file name: ");

                                //reading input file name

                                String inFileName=scanner.nextLine();

                                //opening input file, will throw an exception if file not found

                                Scanner fileScanner=new Scanner(new File(inFileName));

                                //opening an output file for writing

                                PrintWriter outputWriter=new PrintWriter(new File("outSentences.txt"));

                                //looping through all lines

                                while(fileScanner.hasNext()){

                                                //creating a StringBuilder with the line

                                                StringBuilder line=new StringBuilder(fileScanner.nextLine());

                                                //passing text to converter class

                                                converter=new Converter(line);

                                                //getting converted text

                                                String converted=converter.getConvertedText();

                                                //displaying it, and writing to output file

                                                System.out.println(converted);

                                                outputWriter.println(converted);

                                }

                                //closing output file and committing the write operation

                                outputWriter.close();

                }

}

/*OUTPUT*/

Enter file name: inText.txt

The eight eggs were separated into three groups.

Five boys and seven girls were present.

He was one hour and five minutes late.

She ate three dozen doughnuts!

Four dogs were chasing three cats.

The captain said, “This is the zero hour”.

I tried to call you nine times today; Ann tried six times!!

The 12 firemen worked quickly.