将充满数字的文本文档翻译成文字

时间:2014-02-28 02:17:48

标签: java string

我正在进行一项练习,我正在将文本文件中的数字翻译成单词,程序应该在我的NumberTranslator类的translate方法中显示数字,并且它应该从字符串中取出每个单独的数字数字并将其翻译成文本值。

例如,如果我有一个读取的文本文件

1234

12

34

输出结果为:

1234: One Two Three Four

12: One Two

34: Three Four

这是我到目前为止的内容,我相信我已经正确阅读了文件,但我不确定,而且我也不确定我的numberTranslator类中的translate()方法。

package hw05;

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws java.lang.Exception {

}

public static String readfile(String filename) { //reads the file translate.txt
    String content = null;
    File file = new File("translate.txt");
    try {
        FileReader reader = new FileReader("translate.txt");
        char[] chars = new char[(int) file.length()];
        reader.read(chars);
        content = new String(chars);
        reader.close();


    } catch (IOException e) {
    }
    return content;

}
}

这是我的号码翻译班。

package hw05;


public class NumberTranslator extends Main {

private static final String[] words = {"Zero", "One", "Two", "Three",
        "Four", "Five", "Six", "Seven", "Eight", "Nine"};

public String translate(int number){   
    if (number < 10){
        return words[number];
    }
    String result = "";
    while (number >0) {
        if (!"".equals(result)){
            result = "" + result;
        }
        result = words[number % 10] + result;
        number = number / 10;

    } return result;


}

}

5 个答案:

答案 0 :(得分:1)

您可以阅读该文件并将其翻译如下:

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int num = 0;
NumberTranslator myAwesomeTranslator = new NumberTranslator();
while ((line = br.readLine()) != null) {
   try {
      num = Integer.parseInt(line);
      System.out.println(myAwesomeTranslator.translate(num));
   } catch (NumberFormatException e) {
      System.out.println("I thought you were an int :(");
   }
}
br.close();

答案 1 :(得分:0)

试试这个......

public String translate(int number){  
    String numberString = Integer.toString(number);
    String result = "";
    for (int i = 0; i < numberString.length(); i++) {
        char letter = numberString.charAt(i);
        result += words[Integer.parseInt(letter)] + " ";
    } 
    return result;

我还没有测试过,但你应该明白这个想法,而且你必须添加一些异常处理。

答案 2 :(得分:0)

以下是如何做到这一点:

  1. 创建一个数字,其中包含从0到9的单词。
  2. 使用BufferedReader开始阅读文件。
  3. 对于以String获得的每一行,获取length()并开始从0迭代到length - 1。使用charAt()来获取号码。
  4. 使用Integer.parseInt()
  5. 将其解析为整数
  6. 索引数组以获取翻译。

答案 3 :(得分:0)

尝试使用Map,

public static void main(String[] args) throws FileNotFoundException, IOException
    {
        Map<Integer, String> wordMap = new HashMap<Integer, String>();
        wordMap.put(0, "Zero");
        wordMap.put(1, "One");
        wordMap.put(2, "Two");
        wordMap.put(3, "Three");
        wordMap.put(4, "Four");
        wordMap.put(5, "Five");
        wordMap.put(6, "Six");
        wordMap.put(7, "Seven");
        wordMap.put(8, "Eight");
        wordMap.put(9, "Nine");

        String fileName = "location\file";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));

        String content = null;
        while((content = bufferedReader.readLine()) != null)
        {
            for(char number : content.toCharArray())
            {
                System.out.print(wordMap.get(Character.getNumericValue(number))); //write into another file
            }
            System.out.println("");
        }
    }

答案 4 :(得分:0)

对于文本替换,正则表达式非常有用:

private static final String[] words = {" Zero", " One", " Two", " Three",
            " Four", " Five", " Six", " Seven", " Eight", " Nine"};


....

String text = _obtain your text string here_;

for (int i=0; i<10; i++) {
            text = text.replaceAll(String.valueOf(i), words[i]);
}

注意:在数组中,每个单词名称前面都有空格。