Frankly speaking, I have no code about this. I don't know how to start. This should be: If the user inputs 1, the output is its corresponding word "one".
I need some help!
11 5553
Frankly speaking, I have no code about this. I don't know how to start. This should be: If the user inputs 1, the output is its corresponding word "one".
I need some help!
You need to have some code first to get code help. Write down your algorithm first. You may need to use the % operator.
You need to have some code first to get code help. Write down your algorithm first. You may need to use the % operator.
I have now my code, but the problem is: if the user inputs "1500" the output is not "One Thousand Five Hundred", but "1500"and "1Five Hundred"...What is my lacking?? Here's the code - import java.io.*;
-
public class conversion
-
{
-
public static void main (String args[])throws Exception
-
{
-
String number;
-
int amount;
-
-
System.out.print("Enter amount not exceed to two thousand: ");
-
InputStreamReader no= new InputStreamReader(System.in);
-
BufferedReader num=new BufferedReader(no);
-
number=num.readLine();
-
amount=Integer.parseInt(number);
-
-
if (amount>=2000)
-
{
-
amount=amount-2000;
-
System.out.println(number.replaceAll("2000","Two Thousand"));
-
}
-
if (amount>=1000)
-
{
-
amount=amount-1000;
-
System.out.println(number.replaceAll("1000","One Thousand"));
-
}
-
if (amount>=900)
-
{
-
amount=amount-900;
-
System.out.println(number.replaceAll("900","Nine Hundred"));
-
}
-
if (amount>=800)
-
{
-
amount=amount-800;
-
System.out.println(number.replaceAll("800","Eight Hundred"));
-
}
-
if (amount>=700)
-
{
-
amount=amount-700;
-
System.out.println(number.replaceAll("700","Seven Hundred"));
-
}
-
if (amount>=600)
-
{
-
amount=amount-600;
-
System.out.println(number.replaceAll("600","Six Hundred"));
-
}
-
if (amount>=500)
-
{
-
amount=amount-500;
-
System.out.println(number.replaceAll("500","Five Hundred"));
-
}
-
if (amount>=400)
-
{
-
amount=amount-400;
-
System.out.println(number.replaceAll("400","Four Hundred"));
-
}
-
if (amount>=300)
-
{
-
amount=amount-300;
-
System.out.println(number.replaceAll("300","Three Hundred"));
-
}
-
if (amount>=200)
-
{
-
amount=amount-200;
-
System.out.println(number.replaceAll("200","Two Hundred"));
-
}
-
if (amount>=100)
-
{
-
amount=amount-100;
-
System.out.println(number.replaceAll("100","One Hundred"));
-
}
-
if (amount>=90)
-
{
-
amount=amount-90;
-
System.out.println(number.replaceAll("90","Ninety"));
-
}
-
if (amount>=80)
-
{
-
amount=amount-80;
-
System.out.println(number.replaceAll("80","Eighty"));
-
}
-
if (amount>=70)
-
{
-
amount=amount-70;
-
System.out.println(number.replaceAll("70","Seventy"));
-
}
-
if (amount>=60)
-
{
-
amount=amount-60;
-
System.out.println(number.replaceAll("60","Sixty"));
-
}
-
if (amount>=50)
-
{
-
amount=amount-50;
-
System.out.println(number.replaceAll("50","Fifty"));
-
}
-
if (amount>=40)
-
{
-
amount=amount-40;
-
System.out.println(number.replaceAll("40","Forty"));
-
}
-
if (amount>=30)
-
{
-
amount=amount-30;
-
System.out.println(number.replaceAll("30","Thirty"));
-
}
-
if (amount>=20)
-
{
-
amount=amount-20;
-
System.out.println(number.replaceAll("20","Twenty"));
-
}
-
if (amount>=10)
-
{
-
amount=amount-10;
-
System.out.println(number.replaceAll("10","Ten"));
-
}
-
if (amount>=9)
-
{
-
amount=amount-9;
-
System.out.println(number.replaceAll("9","Nine"));
-
}
-
if (amount>=8)
-
{
-
amount=amount-8;
-
System.out.println(number.replaceAll("8","Eight"));
-
}
-
if (amount>=7)
-
{
-
amount=amount-7;
-
System.out.println(number.replaceAll("7","Seven"));
-
}
-
if (amount>=6)
-
{
-
amount=amount-6;
-
System.out.println(number.replaceAll("6","Six"));
-
}
-
if (amount>=5)
-
{
-
amount=amount-5;
-
System.out.println(number.replaceAll("6","Five"));
-
}
-
if (amount>=4)
-
{
-
amount=amount-4;
-
System.out.println(number.replaceAll("4","Four"));
-
}
-
if (amount>=3)
-
{
-
amount=amount-3;
-
System.out.println(number.replaceAll("3","Three"));
-
}
-
if (amount>=2)
-
{
-
amount=amount-2;
-
System.out.println(number.replaceAll("2","Two"));
-
}
-
if (amount>=1)
-
{
-
amount=amount-1;
-
System.out.println(number.replaceAll("1","One"));
-
}
-
}
-
}
RedSon 5,000
Recognized Expert Expert
Woah this code is a nightmare! The problem you are having is because your code is unreadable, it is also going to be very difficult to maintain, what if I was to input 3000 or 4000 or even 10000. You must think of a much more elegant solution to this problem then massive ifs like you have. When your design is better your problem will fix itself.
RedSon 5,000
Recognized Expert Expert
Why did you not take the suggestion of using the % operator?
Why did you not take the suggestion of using the % operator?
tnx for the comment!! I will try % operator..
HI cess
Your idea is great...
But you didnt specify 11 to 19...
I am a newbie in java .. the only thing i found inside that code is that....
and one more thing you didnt entered zero
Why did you not take the suggestion of using the % operator?
Sorry cause i a newbie in java, maybe this is a stupid question,
but may i know what is the % operator or may i know where can get this information?
i try to search at google.com but i can't really get what it means at all.
Thanks.
RedSon 5,000
Recognized Expert Expert
Its call a modulus operator or mod operator, try googling for that instead.
Its call a modulus operator or mod operator, try googling for that instead.
Thanks for the information, i will study it at google.
SammyB 807
Recognized Expert Contributor
Thanks for the information, i will study it at google.
You also want to use a hash table. Here it is in C#. - public static string NumberToWords(int num)
-
{
-
Hashtable htb = new Hashtable();
-
htb.Add(0, "no");
-
htb.Add(1, "one");
-
htb.Add(2, "two");
-
htb.Add(3, "three");
-
htb.Add(4, "four");
-
htb.Add(5, "five");
-
htb.Add(6, "six");
-
htb.Add(7, "seven");
-
htb.Add(8, "eight");
-
htb.Add(9, "nine");
-
htb.Add(10, "ten");
-
htb.Add(11, "eleven");
-
htb.Add(12, "twelve");
-
htb.Add(13, "thirteen");
-
htb.Add(14, "fourteen");
-
htb.Add(15, "fifteen");
-
htb.Add(16, "sixteen");
-
htb.Add(17, "seventeen");
-
htb.Add(18, "eighteen");
-
htb.Add(19, "nineteen");
-
htb.Add(20, "twenty");
-
htb.Add(30, "thirty");
-
htb.Add(40, "forty");
-
htb.Add(50, "fifty");
-
htb.Add(60, "sixty");
-
htb.Add(70, "seventy");
-
htb.Add(80, "eighty");
-
htb.Add(90, "ninety");
-
if (num<0 || num>99) return num.ToString();
-
if (num<20) return htb[num].ToString();
-
if (num%10 == 0) return htb[num].ToString();
-
return htb[(num/10)*10].ToString() + "-" + htb[num%10].ToString();
-
}
This is only for 0-99 because that's all i needed, but it's easy to extend. Just add hundred and thousand to the hash and do a few more DIV' and MOD's. But, sorry, I don't do Java.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dan Stromberg |
last post by:
I've written up a page about how to convert native binary data to
another platform's native binary data, as I did some fortran data
conversions for a client.
The programs and documentation are...
|
by: Al Moritz |
last post by:
Hi all,
I was always told that the conversion of Word files to HTML as done by
Word itself sucks - you get a lot of unnecessary code that can
influence the design on web browsers other than...
|
by: Chris Online |
last post by:
Hi all,
I'm using C++ Builder5. I want to get data from an edit-box and
send it to a development kit. The dev-kit can only receive char
and no char*
here's a part of my code:
char* Data_byte...
|
by: prabha |
last post by:
Hello Everybody,
I have to conert the word doc to multiple html files,according to the templates in the word doc.
I had converted the word to xml.Also through Exsl ,had finished the multiple...
|
by: Asbjørn Ulsberg |
last post by:
Hi.
I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0
module to C#. I've managed to convert the VB code to VB.NET,
which gave me the following code:
Option Strict On
Option...
| |
by: Converting Numeric to a word |
last post by:
Hi,
I have little experience in C#, Can anyone help me with this sproblem. " I want to convert a given number in a text to a word on a lable" for eg if we type 25 in text box , i want to display it...
|
by: AMP |
last post by:
Hello,
I have in c:
WORD calcChecksum(BYTE data, WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;
i_data= (WORD*)data;
|
by: anukagni |
last post by:
Hi friends,
I want to get this thing happen..
I want to convert the number to word i.e. if i entry 1 in an text box then it should give the reflect to One in an lable ..
Is this possible...
|
by: danmilkman |
last post by:
Hi Y'all
Can someone help me convert the next few lines of code from c++ to c#?
I've given it a try myself but my program keeps crashing when I use my c# code. I think the difficulty lies in...
|
by: ganesh22 |
last post by:
Hi,
Iam getting the below error while my application is running on IIS. in my application iam converting a text into word format, so i added some .dll from COM for converting word format
...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |