0

For my current code, I'm creating a word calculator where words are inputted to represent numbers and the calculations are done within the code. The requirements is to input two numbers and an operator into the console. I was able to parse the input into three parts, the first number, the operator, and the second number.

My question is how should I approach when I convert the word into number form? For example, if a user inputted:

seven hundred eighty-eight plus ninety-five

How can I turn that into 788 and 95 so I can do the calculations within the code? My input needs to go up to 1000.

This is part of my code for dividing up the input.

import java.util.Scanner;
public class TextCalc2 {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
String in = input.nextLine();
in = in.toLowerCase();
while (!in.equals("quit")) {
    if (in.contains("plus")){
        String number1 = in.substring(0, in.indexOf("plus") - 1);
        String number2 = in.substring(in.indexOf("plus") + 5, in.length());
        String operator = in.substring(in.indexOf("plus"),in.indexOf("plus") + 5);
        System.out.println(number1);
        System.out.println(operator);
        System.out.println(number2);
    }
}
  • You need to have specific rules for what is considered acceptable input. E.g. is "seventy-one" OK but you wouldn't accept "seventy one"? Frankly, I would just hard code the teens, twenty, thirty, fourty, etc. and then pick out those words. It's pretty easy to identify a number only up to 1,000. – Jared Jan 25 '15 at 01:18
  • 3
    Use this aproach: http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java. But use equals method to find the right number in the array. – bsferreira Jan 25 '15 at 01:19
  • There should be a "-" between the tens and the ones. I was deciding to parse the numbers even further by using number1.contains("hundreds") for example but I realized there would be many different combinations and it took quite some lines just to produce one set. I was wondering if there was an easier way to do this. – LGPiggyDomo Jan 25 '15 at 01:37

1 Answers1

0

First of all there are problems in the way you are splitting the input (you have hard coded the operation). I would suggest splitting your input with " " (space) and then analyzing each part of the input separately.

You will have different types of words in your input, one is a single digit number, double digit number, operations and "hundred".

Then you should find which category first word of your input belongs to, if it has "-" it will be double digit, else look into other categories and search for it till you find it. Then based on category decide what you should do with it, if its a single digit, replace it with its equivalent. if its double digit, split and then replace each digit, if its operation store it and cut your input array from there so that you have separated the first value and second one, and if its hundred multiply previous single digit by 100.

After this parsing steps you will have something like this {"700","88"} , {"95"} and plus operation. now its easy to convert each string to its integer value using parse method and then apply the operation.

BTW, it would be easier to use Enum for your constants and just use their ordinal value. Also use the comment that Jared made for your double digit values.

Let me know if you still have question.

Kayvan N
  • 8,108
  • 6
  • 29
  • 38