Me and my groupmates want to create a program, wherein you input numbers expressed in words like "nine hundred ninety thousand one hundred and fifty" into numerical units like "990,100.50" including the decimals. and wherein a message will show up indicating an error in the input words if ever the users will try to put other words aside from numbered words. We started making this code all day but can't seem to make it work efficiently.
CODE
import javax.swing.*;
import java.util.*;
public class numberWords
{
private static String [] num_words={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
private static int [] num={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};
private static int [] n;
private static int [] arr;
private static double and=0.00;
private static int k,j;
private static void method1()
{
int length1;
System.out.println(n.length);
length1=n.length;
int v=-1;
long sum1=0,sum=0,product=0,product2=0,the_number=0,inter_product=0;
do
{
do
{
v++;
if(v!=n.length)
{
if((n[v]>=0)&&(n[v]<=99))
{
if(v+1!=n.length)
{
if((n[v+1])==100)
{
if((v+2!=n.length)&&(n[v+2]>=0)&&(n[v+2]<=99))
{
if((v+3!=n.length)&&(n[v+3]>=0)&&(n[v+3]<=99))
{
if((v+4!=n.length)&&(n[v+4]>=0)&&(n[v+4]<=99))
{
}
inter_product=(long)((n[v]*100)+n[v+2]+n[v+3]);
sum1=(long)(sum1+inter_product);
v=v+3;
break;
}
inter_product=(long)((n[v]*100)+n[v+2]);
sum1=(long)(sum1+inter_product);
v=v+2;
break;
}
inter_product=(long)(n[v]*100);
sum1=(long)(sum1+inter_product);
v=v+1;
break;
}
if((n[v+1]==1000)||(n[v+1]==1000000)||(n[v+1]==1000000000))
{
if (inter_product!=0)
{
product2=(long)(inter_product*n[v+1]);
sum=(long)(sum+product2);
break;
}
else
{
product2=(long)(n[v]*n[v+1]);
sum=(long)(sum+product2);
v=v+1;
break;
}
}
else
{
break;
}
}
sum=(long)(sum+n[v]);
break;
}
else
{
switch(n[v])
{
case 1000:
if (inter_product!=0)
{
product2=(long)(inter_product*1000);
sum=(long)(sum+product2);
break;
}
else
{
product2=(long)(n[v-1]*1000);
sum=(long)(sum+product2);
break;
}
case 1000000:
if (inter_product!=0)
{
product2=(long)(inter_product*1000000);
sum=(long)(sum+product2);
break;
}
else
{
product2=(long)(n[v-1]*1000000);
sum=(long)(sum+product2);
break;
}
case 1000000000:
if (inter_product!=0)
{
product2=(long)(inter_product*n[v]);
sum=(long)(sum+product2);
break;
}
else
{
product2=(long)(n[v-1]*n[v]);
sum=(long)(sum+product2);
break;
}
default:
break;
}
break;
}
}
else
{
break;
}
}while(v<n.length);
}while(v<n.length);
System.out.println("The number is: "+sum+"\nthe inter_product is: "+inter_product);
JOptionPane.showMessageDialog(null,"The number is: "+sum+"\nthe inter_product is: "+inter_product,"Results",JOptionPane.INFORMATION_MESSAGE);
}
private static int choice(String check,int j1)
{
if(check.equalsIgnoreCase("billion"))
{
multiply(1000000000,j1);
j1++;
return j1;
}
else{
if(check.equalsIgnoreCase("million"))
{
multiply(1000000,j1);
j1++;
return j1;
}else{
if(check.equalsIgnoreCase("thousand"))
{
multiply(1000,j1);
j1++;
return j1;
}else{
if(check.equalsIgnoreCase("hundred"))
{
multiply(100,j1);
j1++;
return j1;
}else{
return j1;
}
}
}
}
}
private static void point(String [] array21)
{
int var=0;
j++;
k=-1;
do
{
k++;
do
{
if((j!=array21.length)&&(k!=num_words.length))
{
if(array21[j].equalsIgnoreCase(num_words[k]))
{
var=(num[k]*10);
k=-1;
do
{
k++;
if(((j+1)!=array21.length)&&(k!=num_words.length))
{
if(array21[j+1].equalsIgnoreCase(num_words[k]))
{
var=var+num[k];
break;
}
else
{
break;
}
}
else
{
break;
}
}while(k<num_words.length);
break;
}
else
{
break;
}
}
else
{
break;
}
}while(k<num_words.length);
}while(k<num_words.length);
if(var==0)
{
JOptionPane.showMessageDialog(null,"Sorry! You did not enter a numbered word!","RESULT!",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println("result: "+var);
}
private static void multiply(int number, int number1)
{
int second,third;
second=number;
third=number1+1;
send(second,third);
}
private static void send(int num1,int index1)
{
int index;
int use;
index=index1-1;
if(n[index]!=0)
n[index+1]=num1;
else
n[index]=num1;
System.out.println(n[index]+" \n:"+n.length+"\nindex is : "+index);
}
public static void main(String args[])
{
int i,q,first=0;
String input;
input=JOptionPane.showInputDialog("Enter a number in words: ");
StringTokenizer string1=new StringTokenizer(input);
//counts the tokens then assigns it as the length of the array
String [] tokens = new String[string1.countTokens()];
int [] arr2=new int[string1.countTokens()];
n=arr2;
//assigns every token to an array element for better application
i=-1;
do
{
i++;
tokens[i]=string1.nextToken();
System.out.println(tokens[i]);
}while(string1.hasMoreTokens());
System.out.println();
//checks if the entered words corresponds to a number
k=-1;//if one thousand one
j=0;
do
{
k++;
do
{
if((j!=tokens.length)&&(k!=num_words.length))
{
if(tokens[j].equalsIgnoreCase(num_words[k]))
{
first=num[k];
j++;
send(num[k],j);
k=-1;
break;
}
else
{
if(tokens[j].equalsIgnoreCase("and"))
{
point(tokens);
break;
}
else
{
j=choice(tokens[j],j);
break;
}
}
}
else
{
break;
}
}while(k<num_words.length);
}while(k<num_words.length);
if(first==-1)
{
JOptionPane.showMessageDialog(null,"Sorry you did not enter a numered word!!!");
System.exit(0);
}
method1();
System.out.print("First: \n"+first+"\nLength: \n"+num_words.length);
}
}
this is the whole code that we made, we would really appreciate some comments, help or guidance. We are currently just beginners in programming and mostly rely on books only, because our professor hasn't taught us that much yet. thanks!