can anyone modify this code so that it can also recognize words in caps and word
ID: 3576470 • Letter: C
Question
can anyone modify this code so that it can also recognize words in caps and words starting with caps please?
import java.awt.Frame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Panel;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.util.Iterator;
import java.awt.Font;
public class Alphabetizer extends Frame implements ActionListener
{
Button b;
TextField tf;
TextArea ta;
Panel p,south,center;
Label l;
Alphabetizer(){
setVisible(true);setSize(700,500);
setFont(new Font("",Font.BOLD,20));
p=new Panel();
l=new Label("Enter Some text");p.add(l);
tf=new TextField(30);p.add(tf);
add(p,"North");
south=new Panel();
b=new Button("Click here to sort words");south.add(b);
add(south,"South");
center=new Panel();
ta=new TextArea(20,20);center.add(ta);
add(center,"Center");
p.setBackground(Color.cyan);
center.setBackground(Color.cyan);
south.setBackground(Color.cyan);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
b.addActionListener(this);
validate();
}
@Override
public void actionPerformed(ActionEvent ae){
String txt=tf.getText();//getting text from text field
TreeSet<String> ts=new TreeSet<String>();
StringTokenizer st=new StringTokenizer(txt);//to divide into words
while(st.hasMoreTokens())
ts.add(st.nextToken());
Iterator<String> i=ts.iterator();
while(i.hasNext())
{
String s=i.next();
ta.append(s+" ");
}
}
public static void main(String args[])
{
new Alphabetizer();
}
}
Explanation / Answer
import java.awt.Frame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Panel;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Iterator;
import java.awt.Font;
public class Alphabetizer extends Frame implements ActionListener
{
Button b;
TextField tf;
TextArea ta;
Panel p,south,center;
Label l;
Alphabetizer(){
setVisible(true);setSize(700,500);
setFont(new Font("",Font.BOLD,20));
p=new Panel();
l=new Label("Enter Some text");p.add(l);
tf=new TextField(30);p.add(tf);
add(p,"North");
south=new Panel();
b=new Button("Click here to sort words");south.add(b);
add(south,"South");
center=new Panel();
ta=new TextArea(20,20);center.add(ta);
add(center,"Center");
p.setBackground(Color.cyan);
center.setBackground(Color.cyan);
south.setBackground(Color.cyan);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
b.addActionListener(this);
validate();
}
@Override
public void actionPerformed(ActionEvent ae){
String textAreaText = ta.getText();
List<String> list = new ArrayList<String>();
if (textAreaText.contains(" ")) {
String[] splitText = textAreaText.split(" ");
for(int i=0; i<splitText.length; ++i)
list.add(splitText[i]);
}
String textFieldText = tf.getText();
list.add(textFieldText);
Collections.sort(list);
ta.setText("");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
ta.append(iterator.next()+" ");
}
}
public static void main(String args[])
{
new Alphabetizer();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.