Hi guys i'm trying to write a simple program using control structures to convert numbers to words but the program is becoming way too long. Is there a simpler way to write it? An example is, if a user inputs 123 the output should be one two three. I didn't complete it but here is a sample:
import java.util.Scanner;
public class Number10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num;
System.out.print("Enter a number and i'll convert it to words: ");
num = input.nextLine();
if((num.length()) == 1)
{
switch(num)
{
case "0":
{
System.out.print("Zero");
break;
}
case "1":
{
System.out.print("One");
break;
}
case "2":
{
System.out.print("Two");
break;
}
case "3":
{
System.out.print("Three");
break;
}
case "4":
{
System.out.print("Four");
break;
}
case "5":
{
System.out.print("Five");
break;
}
case "6":
{
System.out.print("Six");
break;
}
case "7":
{
System.out.print("Seven");
break;
}
case "8":
{
System.out.print("Eight");
break;
}
case "9":
{
System.out.print("Nine");
break;
}
default:
{
System.out.print("Please enter a number");
break;
}
}
}
else if((num.length()) == 2)
{
switch(num)
{
case "11":
{
System.out.print("One One");
break;
}
case "12":
{
System.out.print("One Two");
break;
}
case "13":
{
System.out.print("One Three");
break;
}
case "14":
{
System.out.print("One Four");
break;
}
case "15":
{
System.out.print("One Five");
break;
}
case "16":
{
System.out.print("One Six");
break;
}
case "17":
{
System.out.print("One Seven");
break;
}
case "18":
{
System.out.print("One Eight");
break;
}
case "19":
{
System.out.print("One Nine");
break;
}
case "20":
{
System.out.print("Two Zero");
break;
}
case "21":
{
System.out.print("Two One");
break;
}
case "22":
{
System.out.print("Two Two");
break;
}
case "23":
{
System.out.print("Two Three");
break;
}
case "24":
{
System.out.print("Two Four");
break;
}
default:
{
System.out.print("Please enter a number");
break;
}
}
}
else
{
System.out.print("Invalid number");
}
}
}
Please help me out. Thanks.
I would do this:
import java.util.Scanner;
public class Number10 {
private static String[] nums = new String[]
{"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine"};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num;
System.out.print("Enter a number and i'll convert it to words: ");
num = input.nextLine();
input.close()
for (Character c: num.toCharArray())
System.out.print(nums[Character.getNumericValue(c)]+ ' ');
}
First of all, you could create a
java.util.Map<Integer, String>
holding a mapping of integer values to string representations, but this will only shorten your code. The next step would be to handle numbers by digits, so you might hold a map of digit to string representation and print the string representation for each digit:
Map<Integer, String> numbers = new HashMap<Integer, String>();
numbers.put(0, "Zero");
// 22 = numbers.get(2) + " " + numbers.get(2);
// 51 = numbers.get(5) + " " + numbers.get(1);
// ...
It's much better to do this with a HashMap<Integer,String>
. This makes for cleaner code and for more flexibility. If, for instance, you want to add functionality for other languages as well.
Use a loop to fill the HashMap with .put()
, perhaps from a file. Then use .get()
to retrieve the translations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.