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

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 …

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

this year

.. i will learn new things, perhaps play a new instrument or go back to school.
.. i will find beauty on the open source community.
.. i will read more books and listen to more music.
.. i will pen more poems and possibly write more songs.
.. i will go for new distances and discover new terrains.
.. i will continue to pile running miles to stay fit and healthy.
.. i will strive to be free and make myself available to unknown future.
.. i will go out of my shell more often and be socially more open.

.. i will continue to grow and go with the changes of time.
.. i will be more patient and hopeful to things that may not even be possible.
.. i will be a better son, brother and more importantly a better father.
.. i will be closer with the great beyond.

.. i will continue to repair , refactor and upgrade myself to become a better version of me.