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;
}
one-one? I suggest you get this method working before writing more code.if (number % 10 < 10)is always true.