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.

Popular posts from this blog

Getting Started with Stateless : A Lightweight Workflow Library Alternative for .NET

Image Credit: https://www.pioneerrx.com A year ago, I was looking for a simple workflow manager for a project I was working. Its a medium sized application that involves tracking the state of assets in the system. Back in 2008, Microsoft (MS) introduced new technologies along with the release of Visual Studio 2008: Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and  Windows Workflow Foundation (WF). Having worked in a company utilizing mostly MS products for development, my first option was to go with WF. After doing some time reading and studying the library, I paused and decided it was too complex for my requirement. Using WF would be an overkill and the fact that it has, a rather, steep learning curve, there has to be another option. My mind toyed with the idea of developing a simple workflow library myself. It would be a learning experience but it might end up consuming a lot of time. Why reinvent the wheel? So I started querying the inte

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

Cyber-bullying : The "good", the bad and the ugly

Image courtesy of http://www.digitalesq.com/ Cyber-bullying is defined as  the willful and repeated use of cell phones, computers, and other electronic communication devices to harass and threaten others. With the advent of social media, the incidents has increased in numbers and the victims does not even know what is hitting them. For the past years, we have heard of  depressions and deaths because of this. Yet, there has never been a strong drive to increase public awareness and promote support groups to help victims outside of the schools.  Campaigns and programs has never gained mainstream presence enough to make an impact.