2

A friend mentioned "utility class NumberTranslator" but I'm not sure how to use that.

input will be a file that consists of numbers formatted like this:

30 1 3 150

3 6 30

5 6 9

90

out put should be:

30 : three zero

1 : one

3 : three

150 : one five zero
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
  • 3
    Read each number from the file. Get each digit from the number. Print the English equivalent of the number. For a more specific answer, ask a more specific question. – azurefrog Jun 25 '15 at 22:53
  • 3
    Possible duplicate: http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java – Ryan J Jun 25 '15 at 22:54
  • with out having to make an if statement for every number possibility? just to handle if's for 1-9. how do i break down a number like 150 as three seperate numbers? – Brian Puszkar II Jun 25 '15 at 22:55
  • @BrianPuszkarII You just asked two different question: (1) how to handle each `1-9` to print `one - nine` (2) how to separate digits from numbers. On Stack Overflow try to focus only on one problem at a time (also search before asking). – Pshemo Jun 25 '15 at 22:59
  • @BrianPuszkarII With math! 150 is just 1*100 + 5*10 + 0*1. – azurefrog Jun 25 '15 at 22:59
  • Don't even look at the number 150. All you need are the chars '1', '5', '0'. You shouldn't need to convert those chars into numbers just to get back at the digits. – yshavit Jun 25 '15 at 23:12

1 Answers1

2

This should do the trick:

1) Use Scanner to read next int while file not processed

2) Convert int to character array

3) for each element in char array, use HashMap to get value where key is Character and value is word

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class DigitsConverter{

    // this will have dictionary relation between digits and words
    private static final Map<Character,String> words = new HashMap<>();

    // can use String array instead of Map as suggested in comments
    private static final String[] alsoWords =  {"zero","one","two", "three", "four", "five", "six", "seven", "eight", "nine"}

    // provide mapping of digits to words
    static {

        words.put('0', "zero");
        words.put('1', "one");
        words.put('2', "two");
        words.put('3', "three");
        words.put('4', "four");
        words.put('5', "five");
        words.put('6', "six");
        words.put('7', "seven");
        words.put('8', "eight");
        words.put('9', "nine");
    }


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

           Scanner scanner = new Scanner(new File("../SomeFile"));

           while (scanner.hasNextInt()) {

               char[] chars = ("" + scanner.nextInt()).toCharArray();

               System.out.print(String.valueOf(chars) +": ");

               // for each digit in a given number
               for (char digit: chars) {

                  // print word for that digit
                  System.out.print(words.get(digit) + " ");

                  // if String array is used instead of Map
                  System.out.print(alsoWords[((int)digit- 48)] + " ");

               }
               System.out.println();
           }

           scanner.close();

       }
    }

Edit

Added Pshemo's idea with String array.

John
  • 5,189
  • 2
  • 38
  • 62
  • 5
    I would probably use String array `{"zero", "one", ... ,"nine"}` instead of Map. – Pshemo Jun 25 '15 at 22:56
  • With index `c - '0'`, and appropriate bounds checking of course – yshavit Jun 25 '15 at 23:07
  • @user3360241 in your code where does words.get come from? – Brian Puszkar II Jun 26 '15 at 23:25
  • 1
    @BrianPuszkarII words is a HashMap populated in static initializer block which contains mapping between digits and words. private static final Map words = new HashMap<>(); Note that you may use String[] alsoWords instead of HashMap by subsctracting ascii value of 0 (48) from digit as shown in answer. – John Jun 26 '15 at 23:32
  • @user3360241 ok I'm going the string route and now netbeans is having an issue with .get after alsoWords.get – Brian Puszkar II Jun 26 '15 at 23:37
  • 1
    what issues ? Simply delete all refrences to HashMap words in code if you will use String array. – John Jun 26 '15 at 23:39
  • I'm still pretty new to java and programing, I'm not all that sure what HashMap is.. – Brian Puszkar II Jun 26 '15 at 23:42
  • 1
    It is a collection of (unique) key - value pairs. In this example, key is a digit and value is the word. But simply use String array alsoWords if it makes more sense. – John Jun 26 '15 at 23:46
  • 1
    If the answer addressed your concerns, do feel free to accept it by clicking the grey check mark beside the answer :) – John Jun 27 '15 at 00:07
  • I switched the file input to: ** Scanner fin = null; try { fin = new Scanner(new File("translates.txt")); } catch (FileNotFoundException e) { System.err.println("Error opening the file translates.txt"); System.exit(1); }** and I have the .txt file saved correctly but it wont open the file? any help @user3360241 – Brian Puszkar II Jun 27 '15 at 00:18
  • 1
    Add ../ before file name ---> new Scanner(new File("../translates.txt")) or provide absolute path to file ("C:\\... – John Jun 27 '15 at 00:21
  • @user3360241 that still didn't work. it builds and the error opening file is the only problem – Brian Puszkar II Jun 27 '15 at 00:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81696/discussion-between-brian-puszkar-ii-and-user3360241). – Brian Puszkar II Jun 27 '15 at 00:27