0

I'm fairly new to Java and I've been stuck on a problem in my Java class.

Design and write an applet that searches for single-digit numbers in a text and changes them to their corresponding words. For example, the string “4 score and 7 years ago” would be converted into “four score and seven years ago”. Or if the input were to be "a baseball game has 27 outs" it would become "a baseball game has two-seven outs".

I have the applet created and it runs. I also made it so if a number (between 0 and 99) were entered into the input that it would convert it to text (like 4 to four), but I can't get it to detect numbers within a string like the ones above.
How would I go about making it able to read the string and actually detecting the integers within?

Here's what I have without the GUI components:

private static final String[] numberNames = { "zero", "one", "two", "three", "four",
                                              "five", "six", "seven", "eight", "nine"};
private static String convertNumber(int number) {
      String num;

        if (number % 10 < 10){
          num = numberNames[number % 10];
          number /= 10;
        }
        else {
          num = numberNames[number % 10];
          number /= 10;
        }
        if (number == 0) return num;
        return numberNames[number] + "-" + num;
      }
8
  • 3
    So eleven would be one-one? I suggest you get this method working before writing more code. Commented Apr 28, 2013 at 18:56
  • 2
    There's no difference between the if/else branches. Commented Apr 28, 2013 at 18:57
  • 3
    and if (number % 10 < 10) is always true. Commented Apr 28, 2013 at 18:59
  • Yes, eleven would be one-one, that's how my professor asked it to be formatted. I'll fix that. Commented Apr 28, 2013 at 19:04
  • So, are you actually asking, how to make a Java applet, which can read string from user? The code snippet doesn't really have anything to do with that... Commented Apr 28, 2013 at 19:06

2 Answers 2

1

You don't want to use static final String arrays for something like this. It will work, but the sooner you get used to using enumerations the better a programmer you will be going forward.

NumberEnum.java

public enum NumberEnum {
  ZERO ("zero"),
  ONE ("one"),
  TWO ("two"),
  THREE ("three"),
  FOUR ("four"),
  FIVE ("five"),
  SIX ("six"),
  SEVEN ("seven"),
  EIGHT ("eight"),
  NINE ("nine"),
  TEN ("ten");

  private String text;
  private static final NumberEnum[] elements = NumberEnum.class.getEnumConstants();

  private NumberEnum(String text) {
    this.text = text;
  }

  public String getText() {
    return text;
  }

  public static NumberEnum getFromDigit(int index) {
    if (index < 0 || index > 9) return null;
    return elements[index];
  }
}

DigitReplace.java

public class DigitReplace {
  public static void main(String... args) {
    System.out.println(convertString("4 score and 7 years ago"));
    System.out.println(convertString("3 days ago I ate 173 cheeseburgers"));
  }

  public static String convertString(String input) {
    StringBuilder output = new StringBuilder(input.length() * 2);

    boolean foundOne = false;

    // for every character
    for(char c : input.toCharArray()) {
      // if it's a digit
      if (Character.isDigit(c)) {
        // if this is not the first digit, put a dash
        if (foundOne) {
          output.append('-');
        }

        NumberEnum thisWord = NumberEnum.getFromDigit(c - '0');
        output.append(thisWord.getText()); // 

        foundOne = true;
      } else {
        // just add the character
        foundOne = false;
        output.append(c);
      }
    }

    return output.toString();
  }
}

Output

four score and seven years ago
three days ago I ate one-seven-three cheeseburgers
Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure. If you don't understand how it works feel free to ask.
0

You can use regular expressions to extract the number (read about them) the Pattern and Matcher classes are useful for this.

After that you can use a HashMap to map the numbers to digits like so

LinkedHashMap<Integer, String> numbers = new LinkedHashMap<Integer, String>();
numbers.put(10, "ten");
numbers.put(11, "eleven");
...And so on...

And finally when you have substracted the numbers from the original String, replace them with the replace method

myString.replaceAll(x, numbers.get(x));

Be careful with the above, you should think about replacing whole words just like a text editor.

Hope this guides you well. Read about Regular Expressions, HashMaps or dictionaries (and other stuff like that, you'll see), and make a lot of testing, you will learn from every problem you face while doing this like the little one I described on replacing whole words. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.