-2

I'm working on an exercise right now which includes taking a text file with numbers in it and translating that to words while using exceptions. When my program would read a number such as "1234" from the text file, it would output "1234: One Two Three Four", I'm making two separate classes, a main class and a numberTranslator class, the main class will contain a method translateNumbersInFile(String filename) and that'll open the filename using Scanner tokenizer and the selected file, and my numberTranslator class will contain the translate() method which will translate the strings to separate integers.

I've never used the Scanner tokenizer before and am unfamiliar with opening files with it. Also, not sure how to split the strings up into the separate numbers. Any help is much appreciated, I'm very lost right now..

Just to add: I need to decompose the number into its individual digits, so translating each digit to its textual representation, and then combine them all with the final text string.

Here are my separate classes, thank you.

package hw05;

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


public class Main {


 public static void main(String[] args) {


 }
 public translateNumbersInFile(String filename){

 }

}

And here's my NumberTranslator class, package hw05;

public class NumberTranslator extends Main {

 public String translate(int number){   


 }

}
Tonno22
  • 157
  • 1
  • 9

2 Answers2

1

Start off by reading about scanners and regular expressions, i provided links below.

Have a go at the problem, and ask again when you need more specific help.

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

How can I read input from the console using the Scanner class in Java?

http://docs.oracle.com/javase/tutorial/essential/regex/

http://www.tutorialspoint.com/java/java_regular_expressions.htm

Community
  • 1
  • 1
user2469515
  • 251
  • 3
  • 12
0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class test {
private static final String[] stringarray = { "zero", "one", "two", "three",
        "four", "five", "six", "seven", "eight", "nine" };

public static void main(String[] args) {
    System.out.println("Please enter your file path:");
    Scanner scanner = new Scanner(System.in);
    String filepath = scanner.nextLine();
    // String result = translateNumbersInFile("C:/numtxt.txt");
    String result = translateNumbersInFile(filepath);
    System.out.println(result);
}

public static String translateNumbersInFile(String filepath) {
    StringBuffer sb = new StringBuffer();
    File file = new File(filepath);
    try {
        Scanner sc = new Scanner(file);
        while (sc.hasNextLine()) {
            String oneline = sc.nextLine();
            for (int i = 0; i < 10; i++) {
                oneline = oneline.replaceAll(i + "", " " +      stringarray[i]
                        + " ");
            }
            sb.append(oneline + "\n");
        }
    } catch (FileNotFoundException e) {
        System.out.println("erro filepath!");
        e.printStackTrace();
    }
    return sb.toString();
}
}
freezing
  • 1
  • 2