Results 1 to 5 of 5
  1. #1
    echo33 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Post new to java, converting words into numbers

    Hey all, my first post here

    i have an assignment an i have to convert a user inputted string like:

    "Some possible valid inputs are:

    six thousand
    two thousand, eight hundred
    one thousand and one
    nine thousand, nine hundred and nine
    four hundred
    five hundred and seven
    three"

    However, there will never be anything entered in the tens column, it will always be 0 (zero). Hence, numbers between 10 and 99 inclusive will not be used.

    i am completely lost here and have started my code like this:

    import java.util.*;
    import java.io.*;

    public class WordsToNumbers
    {

    public static void main (String[] args) throws IOException
    {
    String input;
    String hundred;
    String thousand;

    Scanner keyboard = new Scanner(syste.in);

    System.out.print("Please Enter a String");
    input = keyboard.nextLine();

    }
    }

    any ideas?????:confused::confused: thanks heaps

  2. #2
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    First i would make sure all the string is in the same case, then split it into an array, broken up by spaces.
    Then take out any commas, so what you are left with is an array with numbers and values.

    Next cycle through the array to find "thousand" and "hundred".

    If either are found, move back in the array by one element, this will be a number, pass this number through a function which converts it to a int, then times that int by either 1000, or 100.

    A little complicated, I would start by creating a function which takes a number from 0-10 and returns the number written, for example if the number 5 if inputted, "five" would be returned.
    They're 10 people in the world, those who speak binary, and those who dont.
    ^ Lame i know, but i can actually count in it nowadays! :eek:

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If this were my assignment, I would first do a 'tolower' on the inputs. Then I would strip out commas and 'ands', as they carry no information. Then I would split the input on whitespace to form tokens.

    Next, I would push the the token into a stack, and then pop them off. I would create an array of Strings containing "zero", "one"..."nine", and I would compare each token popped off the stack to evry element in that array, adding the index to a running total if found.

    If the token is not in that list, I would compare it to "hundred" and to "thousand", and if found, recursively call the number processing routine on the next stack entry, then multiply by 1000 or 100 as appropriate and add to the running sum.

    If a token fails both of these sets of tests, spit out an error. Once the stack is empty, you have the answer.

  4. #4
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    have some constants set up as well to make it easier, e.g.

    int ONE = 1;int TWO= 2;int THREE = 3;
    int FOUR= 4;int FIVE= 5;int SIX= 6;
    int SEVEN = 7;int EIGHT= 8;int NINE = 9;

    Then have your hundreds etc set up
    int HUNDRED = 00;
    int THOUSAND = 000;
    int TEN = 0;

  5. #5
    Xhile is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default no.

    that wont work. what if they do two hundred and five. wouldnt it be 2005...
    Last edited by Xhile; 04-22-2009 at 09:56 AM. Reason: cause adem is an asshole

Similar Threads

  1. converting jar to .class or .java
    By marmara95 in forum New To Java
    Replies: 12
    Last Post: 10-06-2012, 10:49 PM
  2. need help in converting java to MIDlet
    By bernice in forum Networking
    Replies: 0
    Last Post: 10-21-2008, 05:13 AM
  3. Replies: 1
    Last Post: 07-02-2008, 01:58 PM
  4. The words *game* in java programming :confused:
    By ibmzz in forum Advanced Java
    Replies: 1
    Last Post: 01-23-2008, 09:23 AM
  5. Aspose.Words for Java - 2.1.0.0
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •