0

When this was assign to me I already had something in mind on how to do it, but that "Thing" that I was thinking is to do it manually 1-1000 like so:

import.java.io.*
    public static void main(String[] args) throws IOException
    {
       BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

       int num;

         System.out.println("Enter Numbers to convert: ");
         num=Integer.parseInt(in.readLine());

         if (num==1)
         {
             System.out.println("one");
         }     
         else if(num==2)
         {
             System.out.println("two");
         }
         else if(num==3)
         {
             System.out.println("three");
         }
  \*and so on up to 1000*\

Please help i dont want to do that ! im just a noob programmer :(

Loop Cake
  • 1
  • 2
  • Aren't there any rules you can think of to create such strings? – Pshemo Sep 03 '14 at 14:06
  • 2
    See http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java – Benjamin Sep 03 '14 at 14:07
  • http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java – Łukasz Kosiak Sep 03 '14 at 14:07
  • 1
    try to break number into digits first and then you can do same, just append `hundred`,`thousand`,..... to them according to their position. – Adi Sep 03 '14 at 14:07
  • For numbers ending by 00 to 20, there is no general simple rule but after 20, it becomes quite simple. Try something by decomposing numbers (thousand, hundred) and submit it here if you want more advices. – Julien Sep 03 '14 at 14:16

2 Answers2

0

Got it from here and modified it according to your needs. Credit goes fully to the original owner of this code.

import java.text.DecimalFormat;

public class EnglishNumberToWords {

  private static final String[] tensNames = {
    "",
    " ten",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };

  private static final String[] numNames = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine",
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };

  private EnglishNumberToWords() {}

  private static String convertLessThanOneThousand(int number) {
    String soFar;

    if (number % 100 < 20){
      soFar = numNames[number % 100];
      number /= 100;
    }
    else {
      soFar = numNames[number % 10];
      number /= 10;

      soFar = tensNames[number % 10] + soFar;
      number /= 10;
    }
    if (number == 0) return soFar;
    return numNames[number] + " hundred" + soFar;
  }


  public static String convert(long number) {
    // 0 to 999 999 999 999
    if (number == 0) { return "zero"; }

    String snumber = Long.toString(number);

    // pad with "0"
    String mask = "000000000000";
    DecimalFormat df = new DecimalFormat(mask);
    snumber = df.format(number);

    // XXXnnnnnnnnn
    int billions = Integer.parseInt(snumber.substring(0,3));
    // nnnXXXnnnnnn
    int millions  = Integer.parseInt(snumber.substring(3,6));
    // nnnnnnXXXnnn
    int hundredThousands = Integer.parseInt(snumber.substring(6,9));
    // nnnnnnnnnXXX
    int thousands = Integer.parseInt(snumber.substring(9,12));

    String tradBillions;
    switch (billions) {
    case 0:
      tradBillions = "";
      break;
    case 1 :
      tradBillions = convertLessThanOneThousand(billions)
      + " billion ";
      break;
    default :
      tradBillions = convertLessThanOneThousand(billions)
      + " billion ";
    }
    String result =  tradBillions;

    String tradMillions;
    switch (millions) {
    case 0:
      tradMillions = "";
      break;
    case 1 :
      tradMillions = convertLessThanOneThousand(millions)
         + " million ";
      break;
    default :
      tradMillions = convertLessThanOneThousand(millions)
         + " million ";
    }
    result =  result + tradMillions;

    String tradHundredThousands;
    switch (hundredThousands) {
    case 0:
      tradHundredThousands = "";
      break;
    case 1 :
      tradHundredThousands = "one thousand ";
      break;
    default :
      tradHundredThousands = convertLessThanOneThousand(hundredThousands)
         + " thousand ";
    }
    result =  result + tradHundredThousands;

    String tradThousand;
    tradThousand = convertLessThanOneThousand(thousands);
    result =  result + tradThousand;

    // remove extra spaces!
    return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
  }

  /**
   * testing
   * @param args
   */
  public static void main(String[] args)  {

    try {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter the number");
      int i = scanner.nextInt();
      System.out.println("*** " + EnglishNumberToWords.convert(i));
    }
    catch(Exception e) {

    }
  }
}

A more simple one :

public class ConvertNumberToWords {

    final private  static String[] units = {"Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
        "Sixteen","Seventeen","Eighteen","Nineteen"};
    final private static String[] tens = {"","","Twenty","Thirty","Forty","Fifty",
        "Sixty","Seventy","Eighty","Ninety"};


    public static String convert(Integer i) {
        if( i < 20)  return units[i];
        if( i < 100) return tens[i/10] + ((i % 10 > 0)? " " + convert(i % 10):"");
        if( i < 1000) return units[i/100] + " Hundred" + ((i % 100 > 0)?" and " + convert(i % 100):"");
        if( i < 1000000) return convert(i / 1000) + " Thousand " + ((i % 1000 > 0)? " " + convert(i % 1000):"") ;
        return convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
    }
    public static void main(String[]args){
        for(int i=1;i<1000;i++){
            System.out.println(i+" \t "+convert(i));
        }
    }


}
Sagar D
  • 2,588
  • 1
  • 18
  • 31
0

you have to made numberArray upto thousand count if you want to print string against each read number :P

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

        public static final String[] numberArray = new String[] {
        "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
        "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",     "eighteen", "nineteen"
        };


  BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  String number = in.readLine();

  int num = 0;

  while(num!=1000){
    if (num == Integer.parseInt(number)) {
      System.out.println(numberArray[num]);
    }
    num++;
  }
}
Mohsin
  • 1,586
  • 1
  • 22
  • 44
  • 1
    illegal start of expression >static private final String[] numberArray = new String[] why? – Loop Cake Sep 04 '14 at 13:44
  • Is this works, in your case! or what you need? – Mohsin Sep 04 '14 at 13:46
  • Not if all of 1-1000 shows up into words, what I need is an Idea of a program if you input a number, the number was given will show a result of a numberword or it will spell it out. – Loop Cake Sep 04 '14 at 14:06
  • bro! Have you seen a logic. It is what you need. you input a number. then below down a while loop starts, traverse the whole counting till 1000 from 0. when it matches with the input digit, it only pick that numberword from numberArray, which is spell of that input digit. got it? – Mohsin Sep 05 '14 at 17:45