Home Tutorialhelp Comment Solution (Fixed Errors , special case handled)

 
 

Comment

Raj
Solution (Fixed Errors , special case handled),
September 25, 2008 at 12:15 AM

public class NumberToWords{
static final String[] Number1 = {""," Hundred"};
static final String[] Number2 = {"","One","Two", "Three","Four","Five",
" Six"," Seven", "Eight"," Nine","Ten" };
String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else{

if( (number % 10) == 0){
return Number2[number / 10] + " zero";
}
else{
str = Number2[number % 10];
number /= 10;
if (number == 0) return str ;
return Number2[number] + " " + str;
}

}

if (number == 0) return str;
return Number2[number] + "hundred" + str;
}
public String convert(int number) {
if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + str1;
}
i++;
number /= 100;
}
while (number > 0);
return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
System.out.println("words is :=" + num.convert(13));
System.out.println("words is :=" + num.convert(70));
System.out.println("words is :=" + num.convert(999));
System.out.println("words is :=" + num.convert(20));
}
}
View All Comments | View Tutorial
Related Tutorial and Articles
Programming Tutorials

Java Convert Number to Words
Java Convert Number to Words In this section, you will learn how to convert... to the usage. Through the given code, you can convert the large digit numbers into words...", "ninty" }; public String convert(int number) { int n = 1; int word 

Java Tips and Examples

Convert Number To Words
Convert Number To Words       In this example, We are going to convert number to words. Code...;   }   public String convert(int number 

Java Code Examples

Java Convert date to words
Java Convert date to words In this tutorial, you will learn how to convert date into words. Just like a number, you can also convert a date into words. Here we are going to convert the current date into words. For this, we have created 

General

Java Conversion
to convert number to words.    Degree Converter...Java Conversion      ... will learn to convert decimal number into binary. The java.lang package provides 

Java Interview Questions

Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir 

Java Beginners

Number Convert - Java Beginners
Number Convert  Dear Deepak Sir, I am entered in Command prompt Some value like 234.Value can be convert into Two Hundred and Thirty Four Rupees...=""; while (count != 0){ switch (in){ case 1: num = count % 100; passString(num 

Java Beginners

java program to convert decimal in words
java program to convert decimal in words  write a java program to convert a decimal no. in words.Ex: give input as 12 output must be twelve 

Java-Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 9.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... of raising a number to a power. It can use mathematical functions such as sin, cos 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 12.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...(); // Convert to lower case. str2 = str2.toLowerCase 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 9.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook..., more general conversion formulas can be given in the case where x extends from 

Java Beginners

Distance conversion - Java Beginners
in metres. The program will then present a user menu of conversion types, converting... the user. ? Write three methods to convert the distance to kilometres, feet and inches...{ System.out.println(); System.out.println("1. Convert to kilometers 

Java Beginners

java conversion
java conversion  how do i convert String date="Aug 31, 2012 19:23:17.907339000 IST" to long value 

Java-Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 9.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...;); roman = roman.toUpperCase(); // Convert to upper case letters 

Programming Tutorials

Java Programming: Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... to the output file. The first list contains the words in alphabetical order. The number...; // The word (in lower case letters). int count; // The number 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 2.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the user by name. Before outputting the user's name, convert it to upper case 

Java Tips and Examples

Programming: Count Words - Dialog
Java NotesProgramming: Count Words - Dialog Description Write a program which counts the number of words in text the user enters. Assume that a word... words, and so is "test this". Only count blanks if the last 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 4.2 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... value is the return value of the function. You should count lower case letters 

Programming Tutorials

Java Programming, Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... of (lower case) words in alphabetical order, and at the end of the program the words... case. An alphabetical list of all the words that were found, without 

Programming Tutorials

Java Programming: Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... of words in the user's input, and the number of characters in the user's input... to count the number of characters that are first characters in words. Don't forget 

Java Beginners

convert to java plss - Java Beginners
convert to java plss  hi i am using microsoft VB6.0 and my friend ask me to make java program to convert number to words but i only use visual basic because i dont know much in java plss convert it to me this is my code 

Programming Tutorials

Java Programming: Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... of luck in any case.) Since one quarter is worth 0.25 dollars, the number... the answer as a real number, giving 3.97. The Solution public class 

Java Interview Questions

java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file 

JavaScript Questions

Number conversion in JavaScript
Number conversion in JavaScript  How do you convert numbers between different bases in JavaScript 

General

Java Conversion
 

General

Java: Example - Words to array
Java: Example - Words to array This example shows how to convert words to an array. We will use StringTokenizer to achieve the this.   Some times is is required to words into an array, to solve this you can use 

Java Interview Questions

Please find me a solution that
Please find me a solution that   Write a java program...: System.out.printf("The Area of %s is %s" ,str,result); str = any of these words: Square...' to keep the user running your program until he enters number '5' to exit 

SQL Question

CONVERT VALUE MONEY TO WORDS IN SQL?
CONVERT VALUE MONEY TO WORDS IN SQL?  i want to covert money or varchar value (like 7500000 ) in words like (75 lacs) then how to convert this value in this words . please give me solution 

Java Beginners

Java Conversion Question - Java Beginners
Java Conversion Question   Hello sir ,How i can Convert Java Code to Vb.net ,Plz Help Me 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... be an integral number of dollars. You can convert the user's input 

Java Beginners

HSSFCell to String type conversion
HSSFCell to String type conversion  Hello, Can anyone help me convert HSSFCell type to string. There is a solution in the following URL...://www.roseindia.net/tutorial/java/poi/readExcelFile.html 

Java Beginners

html2xml conversion
html2xml conversion  java code to convert html file to xml 

Java Beginners

Language Conversion
Language Conversion  write a java program to convert "Hello World" in spanish 

Applet Questions

conversion Applet
conversion Applet  I am a java beginner.................. strong texthow to create JApplet that prompts the user to enter a distance in miles... kilometers,where XX.XX is the number of kilometers. A mile is 1.6 kilometers 

General

Convert Byte to Hexadecimal
for this conversion. Code Description: This program takes a byte number from console.... C:\corejava>java ByteToHexa Enter the byte number... Convert Byte to Hexadecimal      

General

Convert Number to Roman Number Using Java
Java Convert Number to Roman Numerals In this section, you will learn how to convert the number to roman number.  To do this, we have created a class... and the method convertIntegerToRoman(int n) convert the specified number into roman 

Java Beginners

Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding 

Java Beginners

Type Conversion - Java Beginners
Type Conversion   Hai, rajanikanth, Please explain how to convert from integer value to a string value .In a database operation i want to convert... for conversion 

Java-Tutorials

Java : String Case Conversion
Java : String Case Conversion In this section we will discuss how to convert a String from one case into another case. Upper to Lower case Conversion : By using toLowerCase() method you can convert any upper case char/String 

Java Tips and Examples

Exercise - Count Words
Java: Exercise - Count Words Problem Write a method which counts the number of words in a string. Assume that a word is defined as a sequence of letters.... countWords("Hello world")2Two words. countWords("Hello, world.")2Still two words 

Java Beginners

Java To Vb.Net Conversion - Java Beginners
Java To Vb.Net Conversion  How to convert Java Code To VB.net,which Software is useful to convert java to vb.net,plz Help Me 

Java Tips and Examples

Convert Number to Binary
Convert Number to Binary       In this section, you will learn to convert a number to a binary (0,1). The given number is a decimal format and the following program to calculate its 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 3.2 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... this is to handle N=1 as a special case before the loop and then to let N go from 2 

Programming Tutorials

Java Programming: Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook..., it will output an error message instead of the number of lines. Since the error is handled..., not just for the "java" command. The Solution /* This program 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 4.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the dice repeatedly, trying to get a given total, the number of rolls it takes can 

Programming Tutorials

Java Programming: Solution to Programming Exercise
POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... { /* This program will convert a given number of eggs... { /* This program will convert a given number of eggs 

General

Convert String to Number
Convert String to Number       In this section, we will learn how to convert String to Number. The following program provides you the functionality to convert String 

Java Beginners

Excel conversion tool. - Java Beginners
Excel conversion tool.  Hi, I need a conversion tool which can convert .xls(Excel 2003) to .xlsx (Excel 2007) and vice-versa. Please suggest any links ro tools. Thank You 

General

Count numbers of spaces and number of words from the input string
Count numbers of spaces and number of words from the input string Java In this section, you will learn how to count the number of spaces and words from... the number of spaces. Now, in order to count the number of words, we have splitted 

Java Technology Tutorials

Java count words from file
Java count words from file In this section, you will learn how to determine the number of words present in the file. Explanation: Java has provides several... by using the StringTokenizer class, we can easily count the number of words 

Programming Tutorials

Java Programming: Solution to Programming Exercise
as a special case. When only one number has been entered, it's certainly the largest... POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... special values to represent infinity and negative infinity. Java has 

Java Beginners

form to java script conversion
form to java script conversion  I am using a form one more form cotain a piece of code . how to convert it to java script to function it properly? code is shown below <form action="http://www.google.com" id="cse-search 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 3.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... The input always consist of a number, followed by an operator, followed by another 

Development process Questions

Conversion - Development process
Conversion Assistant (JLCA). Code that calls Java APIs can convert to comparable C# code...Conversion  Why is there a need to convert from Java to .NET? What is the theory of conversion from java to .NET? How it is done?   Hi 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 3.1 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... report the number of rolls that it makes before the dice come up snake eyes. (Note 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 5.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the average number of rolls. It does this whole process for each possible total (2 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 4.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... for the number of rows, number of columns, and square size.) Set the number 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.1 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the computations. The applet should include a JTextField where the user enters a number 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 10.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... be able to look up a name in the directory to find the associated phone number 

Java Tips and Examples

Programming: Count Words - Dialog
Java NotesProgramming: Count Words - Dialog Description Write a program which counts the number of words in text the user enters. Assume that a word... error-prone1 Hints Blank counter. The number of words it the number 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... Here is my solution. Try clicking and dragging on the applet 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 9.2 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... be stored in an int variable. Java has a standard class, java.math.BigInteger 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... See the solution to Exercise 3.5 for a discussion of how to draw 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 12.2 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... in Java. The mathematical operations on A and B can be computed using methods 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 10.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... possible user interface would be to use a menu, as was done in the solution 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.7 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... Keep track of and display the number of times that the user hits the square 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 11.7 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... or lower case "x". A number must begin with a digit 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.1 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... into Java as follows: int[] numFound; // numFound[i] will be the number 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.6 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... SubKillerGame.java. Modify the game so it keeps track of the number of hits and misses 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 12.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook. Exercise 12.3: The fact that Java has a HashMap class means that no Java 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 11.6 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...;expression> ")" where <x-variable> can be either a lower case 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... and this one Actually, in my solution, I decided to add another case 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.1 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... a clicking sound when the dice are rolled. See the solution to see how 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...-defined responsibility. In this case, the responsibility is "let the user input 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 11.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... that consists of nothing but a leaf. In that case, the number of leaves is 1 

Programming Tutorials

Java Programming, Solution to Programming Exercise
Solution for Programming Exercise 12.6 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... switch(functionCode) { case SIN: return 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 5.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... that the user will go over 21. In that case, the game is over and the user loses 

Java Beginners

java Application Convert to java Applet
java Application Convert to java Applet  Hi every Java Masters i'm Java beginner ,and i want below this figures source code convert to Java Applet...(); System.out.println(); switch(menu) { case 1: System.out.print("Enter Number of Day 

Java Beginners

putting words to line number form a java file/test file and show occurrence
putting words to line number form a java file/test file and show occurrence  hi all i want putting words to line number form a java file and show... and BinarySearchTree and please help me in java coding 

Programming Tutorials

Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... to count how many pieces that player has in a row in that direction. If the number 

Java Beginners

PDF to Word Conversion - Java Beginners
PDF to Word Conversion  Hello, Can we convert a PDF document to Microsoft word document thru Java. If its not possible in Java, is it possible in any other language 

Date Calendar

Java Date conversion - Date Calendar
Java Date conversion  How do I convert 'Mon Mar 28 00:00:00 IST 1983' which is a java.util.Date or String to yyyy/MM/dd date format in sql 

Java Beginners

String to Date Conversion - Java Beginners
)); For read more information : http://www.roseindia.net/java/java-conversion... 11-09-2008 07:32:AM Now I just need to convert this String variable value...  Hi friend, For conversion in this format : 11/SEP/2008 07:07:32 

General

Java Date Conversion
Java Date Conversion In java date conversion, two packages are used... Java Converts String into Date In this example we are going to convert...; used to convert between dates and time fields and the DateFormat class 

Java Interview Questions

Conversion In Collections - Java Interview Questions
Conversion In Collections  Hi Friends, can u give code to convert... Friend, 1)Convert ArrayList To HashMap: import java.io.*; import java.util....)Convert HashMap To ArrayList: import java.util.*; public class 

Java Beginners

from number to word
from number to word  i want to know weather there is any method that can be use in changing value from number to word. Example if i write ten thousand, it will automatically be written as 10000.   Java convert number 

Java Beginners

Another distance conversion - Java Beginners
Another distance conversion  Write an application that reads a distance in metres. The program will then convert the distance to kilometres, feet... a method for each conversion ? toKilometres(), toFeet(), and toInches 

Java Interview Questions

how to count words in string using java
++; } System.out.println("Number of words are: "+count); } } Thanks   Hello...how to count words in string using java  how to count words in string... count=0; String arr[]=st.split(" "); System.out.println("Number 

Java Tips and Examples

Exercise - Count Words
Java: Exercise - Count Words Problem Write a method which counts the number of words in a string. Assume that a word is defined as a sequence of letters.... countWords("Hello world")2Two word. countWords("Hello, world.")2Still two words 

Java Beginners

Reports to pdf conversion
Reports to pdf conversion  I need to convert reports into pdf format using java.. Will i get any sample code 

Java Beginners

pdf to xml conversion
pdf to xml conversion  i want to convert pdf file into xml file.. where i am having a table in pdf file with some headers and some data into it. i want the headers to be the tag of xml file. how can i do that using java? please 

Java Tips and Examples

Convert Hexadecimal number into Integer
Convert Hexadecimal number into Integer ... to convert hexadecimal data into integer. The java.lang package provides the functionally to convert the hexadecimal data into an integer type data.  Code 

General

Number Format Exception
.  A Number Format Exception occurs in the java code when a programmer tries to convert a String into a number. The Number might be int,float or any java... Number Format Exception      

Java Beginners

Conversion from color to Gray Image - Java Beginners
Conversion from color to Gray Image  Hi... Sakthi here.. i am new to java. and i haven't try this so far How to convert the color image to gray scale image in java? could u plz help me out to start the process 

General

Casting (Type Conversion)
are there in java. Some circumstances requires automatic type conversion, while in other cases... Java performs automatic type conversion when the type of the expression... Casting (Type Conversion)       

Java Beginners

A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer 

Java Beginners

A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer 

Java Beginners

A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer 

Java Beginners

A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer 

Java Beginners

A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer 

Java Tips and Examples

Example - Read Words
Java Notes: Example - Read Words The program below reads a text file and lists the words alphabetically. GUI interface and model 1 2 3 4.../ReadWordsGUI.java // Purpose: Read a file and display all words in it. // Author