String Problem For each numerical value 0, 1, 2, …9 (0 <= NUMBER <= 9), embedded
ID: 3810158 • 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: constructors constructors charAt( ) charAt( ) indexOf( ) 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 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
package chegg;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
class Converter {
private String convertedString;
public Converter(String inputString) {
this.convertedString = this.doconverion(inputString);
}
private String doconverion(String inputString) {
String actualString = "";
int maxLimit = 0;
int minLimit = 0;
if (inputString == null)
return inputString;
boolean isSpaceIdentified = false;
// main loop
String token = "";
for (int indx = 0; indx < inputString.length(); indx++) {
if (inputString.charAt(indx) == ' ') {
isSpaceIdentified = true;
maxLimit = indx;
}
if (isSpaceIdentified) {
token = inputString.substring(minLimit, maxLimit).trim();
try {
int value = Integer.parseInt(token);
if (value >= 0 && value <= 9) {
token = Converter.convertedSentance(value);
}
actualString = actualString + token + " ";
} catch (NumberFormatException ne) {
actualString = actualString + token + " ";
}
token = "";
minLimit = maxLimit;
isSpaceIdentified = false;
}
}
System.out.println(actualString);
return actualString;
}
private static String convertedSentance(int charAt) {
int choice = charAt;
String result = "";
switch (choice) {
case 1:
result = "One";
break;
case 2:
result = "Two";
break;
case 3:
result = "Three";
break;
case 4:
result = "Four";
break;
case 5:
result = "Five";
break;
case 6:
result = "Six";
break;
case 7:
result = "Seven";
break;
case 8:
result = "Eight";
break;
case 9:
result = "Nine";
break;
default:
result = "" + charAt;
}
return result;
}
public String getConvertedString() {
return this.convertedString;
}
}
public class Driver {
public static void main(String[] args) {
System.out.println("Enter the file name");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.next();
try (BufferedReader br = new BufferedReader(new FileReader("d:\"
+ fileName + ".txt"))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
Converter convertor = new Converter(sCurrentLine);
System.out.println("" + convertor.getConvertedString());
Driver.writeFile(new File("d:\outSentences.txt"),
convertor.getConvertedString());
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
/**
*
*/
private static void writeFile(File fileName, String content) {
System.out.println(content + "/" + fileName);
if (content == null || content.length() < 0)
return;
try (FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Input
--------
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.
Output
---------
The Eight eggs were separated into Three
Five boys and Seven girls were
He was One hour and Five minutes
She ate Three dozen
Four dogs were chasing Three
The captain said, “This is the 0 hour”. I tried to call you 9 times today; Ann tried Six
The 12 firemen worked