Skip to main content

playing around with simple java application..

Someone asked me to make a simple program for converting number to words , but no one came back to get it so decided to publish it out of trippings..

Heres the java code for it..play around and enjoy ( Net Beans 3.6 is an acceptable java IDE)

The Code

package NumConverter;

/* To make use of Dialog boxes in Input and Output*/
import javax.swing.JOptionPane;

public class NumCon {
/*Array members to be used in conversion*/
private String[] strOnes={""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
private String[] strSpecial={" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
private String[] strTensSpecial={""," Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
private String[] strPlaces={""," Tens"," Hundred"," Thousand"," Thousand"," Hundred"," Million"," Million"," Hundred"," Billion"};
public NumCon() {
}
public static void main(String[] args) {
/*instantiate a NumCon class*/
NumCon nC=new NumCon();
int iInput=0; //stores the integer input
String strInput=""; //stores the integer input converted to string
strInput=JOptionPane.showInputDialog("Enter the value :");
if(strInput.length()<=9 && strInput.length()>=1){
iInput=Integer.parseInt(strInput); //get the input
strInput=Integer.toString(iInput); //convert to string
JOptionPane.showMessageDialog(null,nC.genWords(strInput)); //generate the conversion
}
else JOptionPane.showMessageDialog(null,"Limited to 9 Digits and at least 1 Digit only.");
System.exit(0); //normally exit
}
private String genWords(String pstrVal){
char cArr[]=pstrVal.toCharArray();
String strW="";
int iPos=pstrVal.length()-1;
for(int i=0;i<pstrVal.length();i++){
char c=cArr[i];
if(iPos==0 && pstrVal.length()==1){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
}
if(iPos==1 || iPos==4 || iPos==7){
/*checks if the 2nd digit is one*/
if(c!="1"){
/*e.g. 51-- FIFTY + ONE */
strW+=strTensSpecial[Integer.parseInt(Character.toString(c))];
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i+1]))];
}
else{
/*e.g. 12-- TWELVE */
strW+=strSpecial[Integer.parseInt(Character.toString(cArr[i+1]))];
}
}
if(iPos==2 || iPos==5){
if(c!="0"){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
}
if(iPos==3){
if(pstrVal.length()==4){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else{
if((Integer.parseInt(Character.toString(cArr[i])) +Integer.parseInt(Character.toString(cArr[i+1]))+Integer.parseInt(Character.toString(cArr[i+2])))!=0)
strW+=strPlaces[iPos];
}
}
if(iPos==6){
if(pstrVal.length()==7){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
if(iPos==8 ){
if(pstrVal.length()==9){
strW+=strOnes[Integer.parseInt(Character.toString(cArr[i]))];
strW+=strPlaces[iPos];
}
else strW+=strPlaces[iPos];
}
iPos--;
}
if(strW.trim().length()==0) strW="Zero";
return strW;
}
}

Note: The codes are not properly aligned because of the publishing..please copy the code and do the restructuring to make the code more readable and easier to debug.

Comments

This comment has been removed by a blog administrator.
Chuck Reynolds said…
Still Struggling To Get Your Affiliate Checks?

Don't Be A Loser - Click Here:

grocery shopping list

Popular posts from this blog

Hiding Unwanted Python Folders and Files in Visual Studio Code

Visual Studio Code is a universal editor and pretty good at it. However, the explorer view maybe cluttered with the automatically generated folders and files confusing developers. Python is no different. Below are example files and folders generated by Python.

The __pycache__ folder and *.pyc files  are totally unnecessary to the developer. To hide these files from the explorer view, we need to edit the settings.json for VSCode. Add the folder and the files as shown below:
Copy and paste the lines below :

"**/*.pyc":{"when":"$(basename).py"},"**/__pycache__":true

renaming default namespaces for VSTO projects in VS2008

So here is the scenario , you are starting a VSTO project and decided that your default namespace is ExcelAddInTesterApp . You created the project and started coding the project. After several days , your boss called and said "hey marvin , make use of this namespace OurCompany.ExcelAddInTesterApp , we have to add our company name to it got it?" . You get back to your machine thinking its just a simple property just like any project you've been working on. So you right clicked the VSTO project and hit properties . Boom! What the F@#$? The default namespace textbox is disabled!!!!



I've been through this and I googled for ways to do it and ended up with a blog from a Microsoft MVP telling me it can't be done because it is disabled. Then I thought of Refactoring, the beauty and grandeur of the renaming process. I selected the namespace and hit the refactor menu hoping that this would solve the problem . Unfortunately , it did not rather it displayed the message box …

Automatic Properties and Object Initializers in .Net 3.5

With the release of .Net 3.5 alongside with Visual Studio 2008 , new enhancements was again introduced . Some maybe well pronounced such as the inclusion of WCF, WPF , LINQ in .Net 3.0 and some just came unnoticed. If you have been accustomed of using a particular method or technique in implementing a certain code in .Net 2.0 , because of backward compatibility , you may not even notice that there are new ways of implementing it in .Net 3.5.

Here are two new concepts in .Net 3.5 that a developer may not notice ( at least in my opinion ) : Automatic Properties and Object Initializers . To illustrate these two , I am going to present the pre-.Net 3.5 way (.Net 2.0) and the .Net 3.5 way in creating a simple class with simple properties.

Automatic Properties

Creating a class can be tedious , especially when working with a list of properties , . One way to get around having to type the code for a private field and its public property getter and setter is to use a refactoring tool. However, …