Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you're asking is not particularly difficult, but gets trickier as you increase the complexity. Depending on how much you need to understand, this may become very complicated. However for simple <code>number plus number</code> or <code>number minus number</code>, it's reasonably easy. To get you started, the following code will be able to deal with these two scenarios. Feel free to expand it as you wish. Also note that it has minimal error checking - in a production system you'll need quite a bit more of it.</p> <pre><code>import java.util.Map; import java.util.HashMap; public class Nums { private static Map&lt;String, Integer&gt; nums = new HashMap&lt;String, Integer&gt;(); public static void main(String[] args) { nums.put("zero", 0); nums.put("one", 1); nums.put("two", 2); nums.put("three", 3); nums.put("four", 4); nums.put("five", 5); nums.put("six", 6); nums.put("seven", 7); nums.put("eight", 8); nums.put("nine", 9); nums.put("ten", 10); nums.put("eleven", 11); nums.put("twelve", 12); nums.put("thirteen", 13); nums.put("fourteen", 14); nums.put("fifteen", 15); nums.put("sixteen", 16); nums.put("seventeen", 17); nums.put("eighteen", 18); nums.put("nineteen", 19); nums.put("twenty", 20); nums.put("thirty", 30); nums.put("forty", 40); nums.put("fifty", 50); nums.put("sixty", 60); nums.put("seventy", 70); nums.put("eighty", 80); nums.put("ninety", 90); String input = args[0].toLowerCase(); int pos; String num1, num2; int res1, res2; if((pos = input.indexOf(" plus ")) != -1) { num1 = input.substring(0, pos); num2 = input.substring(pos + 6); res1 = getNumber(num1); res2 = getNumber(num2); System.out.println(args[0] + " =&gt; " + res1 + " + " + res2 + " = " + (res1 + res2)); } else if((pos = input.indexOf(" minus ")) != -1) { num1 = input.substring(0, pos); num2 = input.substring(pos + 7); res1 = getNumber(num1); res2 = getNumber(num2); System.out.println(args[0] + " =&gt; " + res1 + " - " + res2 + " = " + (res1 - res2)); } else { System.out.println(args[0] + " =&gt; " + getNumber(args[0])); } } private static int getNumber(String input) { String[] parts = input.split(" +"); int number = 0; int mult = 1; String fact; for(int i=parts.length-1; i&gt;=0; i--) { parts[i] = parts[i].toLowerCase(); if(parts[i].equals("hundreds") || parts[i].equals("hundred")) { mult *= 100; } else if(parts[i].equals("thousands") || parts[i].equals("thousand")) { if(number &gt;= 1000) { throw new NumberFormatException("Invalid input (part " + (i + 1) + ")"); } mult = 1000; } else if(parts[i].equals("millions") || parts[i].equals("million")) { if(number &gt;= 1000000) { throw new NumberFormatException("Invalid input (part " + (i + 1) + ")"); } mult = 1000000; } else if(!nums.containsKey(parts[i])) { throw new NumberFormatException("Invalid input (part " + (i + 1) + ")"); } else { number += mult * nums.get(parts[i]); } } if(!nums.containsKey(parts[0])) { number += mult; } return number; } } </code></pre> <p>This code handles numbers from 0 to 999,999,999 and doesn't handle negative numbers. Again, it shouldn't be too difficult to extend it to increase the range or handle negative numbers. Note that if you extend it to handle billions, you may need to switch from <code>integer</code> to <code>long</code> variables to hold the results.</p> <p>Here are some test runs:</p> <pre><code>$ java Nums "three hundred nineteen million five hundred twenty three thousand six hundred eighteen" three hundred nineteen million five hundred twenty three thousand six hundred eighteen =&gt; 319523618 $ java Nums "five hundred minus three hundred ninety nine" five hundred minus three hundred ninety nine =&gt; 500 - 399 = 101 $ java Nums "thirty three plus seventeen" thirty three plus seventeen =&gt; 33 + 17 = 50 $ java Nums zero zero =&gt; 0 $ java Nums "one plus three" one plus three =&gt; 1 + 3 = 4 $ java Nums "hundred thousand" hundred thousand =&gt; 100000 $ java Nums "hundred thousand minus ten thousand" hundred thousand minus ten thousand =&gt; 100000 - 10000 = 90000 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload