For each numerical value 0, 1, 2, ...9 (0 <= NUMBER <= 9), embedded in a sentenc
ID: 3585035 • Letter: F
Question
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 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.
Explanation / Answer
Converter.java
package syemmetric;
public class Converter {
String[] CapitalMap = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
String[] smallMap = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
StringBuilder input;
String output;
public Converter() {
input = new StringBuilder(" ");
}
public static boolean isNumeric(String str)
{
try
{
int d = Integer.parseInt(str);
}
catch (NumberFormatException nfe)
{
return false;
}
return true;
}
public String get(String line) {
input = new StringBuilder("");
String[] words = line.split(" ");
;
int i, temp;
for (i = 0; i < words.length; i++) {
if (i == 0 && isNumeric(words[i]) && words[i].length() == 1) {
temp = Integer.parseInt(words[i]);
input.append(CapitalMap[temp]);
}
else if (isNumeric(words[i]) && words[i].length() == 1) {
temp = Integer.parseInt(words[i]);
input.append(smallMap[temp]);
} else {
input.append(words[i]);
}
input.append(" ");
}
output = input.toString();
return output;
}
}
Driver.java:
package syemmetric;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the input file : ");
FileReader inputFile = new FileReader(sc.next());
BufferedReader br = new BufferedReader(inputFile);
System.out.print("Enter the name of the output file: ");
FileWriter outputFile = new FileWriter(sc.next());
BufferedWriter bw = new BufferedWriter(outputFile);
String line;
Converter C = new Converter();
while ((line = br.readLine()) != null) {
String outline = C.get(line);
System.out.println(outline);
bw.write(outline + " ");
}
br.close();
bw.close();
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.