Hello. I'm new to Java. I created 4 CHOICE that has 9 numbers. It should convert the selected item into words in the text area. I don't have any idea how to do it. Please Help. Here's my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Final1 extends Frame implements ItemListener{

Final1(){
super("Choice and Text Area");

for (int i=0; i<10; i++) {
    c1.addItem(" "+i);
    c2.addItem(" "+i);
    c3.addItem(" "+i);
    c4.addItem(" "+i);
}

Panel p1=new Panel();
p1.setLayout(new FlowLayout());
p1.add(c1); 
p1.add(c2);
p1.add(c3);
p1.add(c4);
Panel p2=new Panel();
p2.setLayout(new GridLayout(2,2));
p2.add(p1);
p2.add(ta1);
setLayout(new BorderLayout());
add("North",p2);

c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);

}

public void itemStateChanged(ItemEvent arg0) {

}

public static void main(String args[]){

Final1 frame=new Final1();
frame.setSize(300,150);
frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}});
frame.setLocation(251,226);
frame.setResizable(false);
frame.show();

}

}
Inline Code Example Here`

Recommended Answers

All 3 Replies

convert the selected item into words in the text area.

Can you give an example?

Well, this is what i am working on right now. The first two choice is working already. I'm having a hard time to convert it to -teen and -ty.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Final1 extends Frame implements ItemListener{

Panel p1;
GridBagLayout g1;
GridBagConstraints gbc;
String string;


 String st1[] = { "", "one", "two", "three", "four", "five", "six", "seven",
         "eight", "nine", };
 String st2[] = { "hundred", "thousand"};
 String st3[] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
         "fifteen", "sixteen", "seventeen", "eighteen", "ninteen", };
 String st4[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
         "eighty", "ninty" };
Choice c1=new Choice();
Choice c2=new Choice();
Choice c3=new Choice();
Choice c4=new Choice();
TextArea ta1=new TextArea(1,5);

public String convert(int number) {
    int n = 1;
    int word;
    string = "";
    while (number != 0) {
            switch (n) {
            case 1:
                    word = number % 10;
                    if (word != 0) {
                            show(" ");
                            show(st2[0]);
                            show(" ");
                            pass(word);
                    }
                    number /= 10;
                    break;
            }
            n++;
    }
    return string;

}
public String convert1(int number){
int n = 1;
int word;
string = "";
while (number != 0) {
switch (n) {
case 1:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[1]);
show(" ");
pass(word);
}
number /= 100;
break;
}
n++;
}
return string;
}
public String convert2(int number){
int n=1;
int word;
string="";
while (number !=0){
switch(n){
case 1:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[2]);
show(" ");
pass(word);
}
number /= 100;
break;
}
n++;
}
return string;
}
public String convert3(int number){

    return string;

}
public String convert4(int number) {
int n = 1;
int word;
string = "";
while (number != 0) {
switch (n) {
case 1:
word = number % 100;

                    pass(word);
                    number /= 100;
                    break;
            }
            n++;
    }
    return string;

}

public void pass(int number) {
int word, q;
if (number < 10) {
show(st1[number]);
}
if (number > 9 && number < 20) {
show(st3[number - 10]);
}

    if (number > 19) {
            word = number % 10;
            if (word == 0) {
                    q = number / 10;
                    show(st4[q - 2]);
            } else {
                    q = number / 10;
                    show(st1[word]);
                    show(" ");
                    show(st4[q - 2]);
            }
    }

}

public void show(String s) {
String st;
st = string;
string = s;
string += st;
}

private String number(int n) {
// TODO Auto-generated method stub
return null;
}
Final1(){
super("Choice and Text Area");

for (int i=0; i<10; i++) {
    c1.addItem(" "+i);
    c2.addItem(" "+i);
    c3.addItem(" "+i);
    c4.addItem(" "+i);
}

Panel p1=new Panel();
p1.setLayout(new FlowLayout());
p1.add(c1); 
p1.add(c2);
p1.add(c3);
p1.add(c4);
Panel p2=new Panel();
p2.setLayout(new GridLayout(2,2));
p2.add(p1);
p2.add(ta1);
setLayout(new BorderLayout());
add("North",p2);

c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);

}

public void itemStateChanged(ItemEvent arg0) {

Final1 w = new Final1();
int i= c1.getSelectedIndex();
String inwords = w.convert1(i);
int j=c2.getSelectedIndex();
String inwords1=w.convert(j);
int k=c3.getSelectedIndex();
String inwords2=w.convert2(k);
int l=c4.getSelectedIndex();
String inwords3=w.convert4(l);

ta1.setText(" " + inwords +inwords1 +inwords2 +inwords3);

}

public static void main(String args[]){

Final1 frame=new Final1();
frame.setSize(300,150);
frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);}});
frame.setLocation(251,226);
frame.setResizable(false);
frame.show();

}

}

For example:

Choice 1 = 2
Choice 2 = 1
Choice 3 = 0
Choice 4 = 0

Two thousand one hundred

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.