Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Extracting Numbers in Word Format From A String of Text

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extracting Numbers in Word Format From A String of Text

    Hey guys,

    So what I am trying to do is extract numbers that are in word format in a long String, i.e. a song, and return each of their numerical values, in order to add them all up. So I'd like to calculate the sum of all of the numbers in the text. This has to work for any piece of text and for all numbers up to a trillion.

    So I broke the string down into tokens and stored them in a String []. And I divided up the possible numbers in word format into:

    LARGEST: thousand, million, billion, trillion
    HUNDRED: hundred
    TENS: twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety
    UNITS: one, two, three, four, five, six, seven, eight, nine
    SPECIALS: ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen

    I believe that these are the only words that it will need to recognize. I began reading the tokenized string from right to left and then when I came across a unit, special or tens as the first number I hit, I would then set it's numerical value and check if the word before was also a number and whether to add or multiply etc. i.e. First number hit is a two, if the number before is sixty, then I would just add it to sixty and check the word before that and so on. However, when implementing it, it seems like an extremely long way around it. Does anyone have any ideas of how I could implement this in a swifter manner? Thanks for any replies, it is greatly appreciated and if I am not making it clear what I need to do then please say so. An example of it working would be: "Nine Million rockets turned Three times and met Twenty Two Aliens", it would extract, Twenty Two as 2, then 20 = 22, then extract Three as 3, and then Nine Million as 1,000,000 x 9 = 9,000,000

    9,000,000 + 22 + 3 = 9,000,025


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Extracting Numbers in Word Format From A String of Text

    Are you asking us how to do this (if so what are you confused about?), or are you asking us to critique your code (if so can you post your code?)?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extracting Numbers in Word Format From A String of Text

    I was just asking for suggestions on how to do this as my method seems to be an extremely long way around it and I am limited in time. I am not looking for someone to do it for me, just a suggestion on a method to do so that I have not thought of or come across before. Thanks.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Extracting Numbers in Word Format From A String of Text

    There are a bunch of different ways to do this. If your way works, I wouldn't worry too much about it.

    But if you're just curious about alternative approaches, the first thing that occurs to me is creating a Map of Strings like thousand, million, hundred, etc, to their numeric counterparts, then parsing through the String and using the Map to convert to a number.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  2. need help converting numbers to word
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 09:02 AM
  3. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  4. Extracting the " (Double Quote) character from a string
    By jai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 28th, 2011, 08:57 AM
  5. help with text format codes
    By vanchan09 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 04:12 PM