import java.util.*;
public class Convert
{
	Scanner in()
	{	return new Scanner(System.in);	}

	void out(String m)
	{	System.out.print(m);	}

	long readLong(String m)
	{
		out(m);
		return in().nextLong();
	}

	int readInt(String m)
	{
		out(m);
		return in().nextInt();
	}

	String readString(String m)
	{
		out(m);
		return in().nextLine();
	}

	String digitOne(int n)
	{
		String st="";
		switch(n)
		{
			case 1:
				st="One";
			case 2:
				st="Two";
			case 3:
				st="Three";
			case 4:
				st="Four";
			case 5:
				st="Five";
			case 6:
				st="Six";
			case 7:
				st="Seven";
			case 8:
				st="Eigth";
			case 9:
				st="Nine";
			case 10:
				st="Ten";
			case 11:
				st="Eleven";
			case 12:
				st="Twelve";
			case 13:
				st="Thirteen";
			case 14:
				st="Fourteen";
			case 15:
				st="Fifteen";
			case 16:
				st="Sixteen";
			case 17:
				st="Seventeen";
			case 18:
				st="Eighteen";
			case 19:
				st="Nineteen";
		}
		return st;
	}

	String digitTwo(int n)
	{
		String st="";
		switch(n)
		{
			case 2:
				st="Twenty";
			case 3:
				st="Thirty";
			case 4:
				st="Forty";
			case 5:
				st="Fifty";
			case 6:
				st="Sixty";
			case 7:
				st="Seventy";
			case 8:
				st="Eighty";
			case 9:
				st="Ninety";
		}
		return st;
	}

	String digitThree(int n)
	{
		String st="";
		if (n==0)
			st="";
		else
			st=digitOne(n)+"Hundred";
		return st;
	}

	String digitFour(int n)
	{
		String st="";
		if (n==0)
			st="";
		else
			st=digitOne(n)+"Thousand";
		return st;
	}

	String digitFive(int n)
	{
		String st="";
		if (n<20)
			st=digitOne(n)+digitFour(n);
		else
			st=digitTwo(n)+digitFour(n);
		return st;
	}

	String digitSix(int n)
	{
		String st="";
		st=digitThree(n)+digitFour(n);
		return st;
	}

	String digitSeven(int n)
	{
		String st="";
		st=digitOne(n)+"Million";
		return st;
	}

    public Convert()
    {
    	int n=readInt("Enter your value: ");
    	String result="";
    	if(n<20)
    		result=digitOne(n);
    	else if (n<100)
    		result=digitTwo(n);
    	out("Result: "+ result+"\n");
    }

    public static void main (String[] args)
    {	new Convert();	}
}

I want to convert string when i input the number.

Recommended Answers

All 3 Replies

You would better make use of arrays to avoid such huge switches.

So then what because i'm just a java beginner so that i couldn't use by array. Hope you are a good Friend :).

Just an easy example:

String[] digits = {"", "one", "two", "three", "four", "five", "six",
                   "seven", "eight", "nine"};
int number = 5;
System.out.println( digits[number] );

I hope you notice the big advantage of doing it this way :)

Analog to this, create arrays for the tens, etc.
But don't put numbers like 25 in the array, as you can create it's string representation by just combining "twenty" and "five".
It's just impossible to hard-code the text-representation for every number in your code, it will take you hours of typing work, unless you simply use arrays and create the numbers by just combining and putting together the base numbers from which you can derive and build every number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.