String processing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YASIN786
    New Member
    • Feb 2007
    • 12

    #1

    String processing

    hi all
    i am helping a friend to develop a java program to implement the following features but i cannot understand how to go about this problem.

    A text file contains a number of English sentences and each sentence occupies one line in the
    file. Develop a Java class which provides the following operations:
    Reads all the sentences from the text file and adds them into an array list.
    Given a word, lists all those sentences which contain the given word.
    Lists all those sentences which have a maximum number of words, in alphabetical order.
    Lists all those sentences which have no words in common.

    I have tried to load the text file first in the text area and then load it into the arraylist (using string tokenizers) so that the user knows wat is goin on but this is not working. I am new to array lists hence donot know how to use them to perform such an operation. the internet only shows how to decalre them and how to add/remove elements from the code.

    CODE:
    [code=java]
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.util.Array List;
    import java.awt.event. *;
    import java.io.Buffere dReader;
    import java.io.FileRea der;
    import java.io.IOExcep tion;
    import java.util.Array List;
    import java.util.Strin gTokenizer;
    import javax.swing.JOp tionPane;

    public class Task2a1 extends javax.swing.JFr ame {
    Button btnLoad;
    Button btnLoadA;
    Button btnExit;
    Label lab;
    TextArea txttext;
    TextArea list1;
    public static void main(String args[])throws IOException {
    Task2a1 app = new Task2a1();
    }
    public Task2a1() {
    super("STRING PROCESSING");
    setBackground(C olor.black);

    //button panel
    Panel buttons = new Panel();
    buttons.add(btn Load=new Button("Load File"));
    buttons.add(btn LoadA=new Button("Load ArrayList"));
    buttons.add(btn Exit=new Button("Exit")) ;
    btnLoad.setBack ground(Color.re d);
    btnLoadA.setBac kground(Color.r ed);
    btnExit.setBack ground(Color.re d);
    add("South",but tons);

    //label panel
    Panel label = new Panel();
    label.setBackgr ound(Color.gray );
    label.add(lab = new Label("STRING PROCESSING"));
    add("North",lab el);
    //textarea panel
    Panel textarea = new Panel();
    setBackground(C olor.black);
    textarea.add(tx ttext= new TextArea(10,75) );
    textarea.add(li st1 = new TextArea(10,75) );
    add("Center",te xtarea);

    pack();
    resize(1024,768 );
    show();
    }
    public boolean handleEvent(Eve nt event) {

    //writing to file
    if(event.id==Ev ent.ACTION_EVEN T){
    if(event.target instanceof Button){
    if("Load File".equals(ev ent.arg)){

    String str="";
    try{
    BufferedReader in = new BufferedReader( new FileReader("Eng lish.txt"));
    while ((str = in.readLine()) != null) {
    txttext.append( str);
    }
    in.close();


    }
    catch (IOException y) {
    txttext.setText ("INVALID FILE");
    }

    }
    else if(event.id==Ev ent.ACTION_EVEN T){
    if(event.target instanceof Button){
    if("Exit".equal s(event.arg)){
    setVisible(fals e);
    }
    }
    }
    else if(event.id==Ev ent.ACTION_EVEN T){
    if(event.target instanceof Button){
    if("Load ArrayList".equa ls(event.arg)){
    ArrayList<Strin g> input1 = new ArrayList<Strin g>();
    ArrayList<Strin g> output = new ArrayList<Strin g>();
    String delim1 = " -,.'";
    StringTokenizer st = new StringTokenizer (txttext.getTex t(), delim1);
    while (st.hasMoreToke ns()) {
    System.out.prin tln(st.nextToke n());
    // input1.add(st.n extToken());
    }
    int n = input1.size();
    for(int i = 0; i < n ; i++)
    System.out.prin tln( input1.get( i ) );
    }
    }
    }

    }
    }

    return true;
    }
    }[/code]

    Any ideas on how to go about this will highly be appreciated.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    First off, don't mix Swing with AWT. Use JButtons instead of Buttons and JLabels instead of Labels, since you have a JFrame. For ArrayList documentation, look here for Sun's API. Every method available is here, along with what it does.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Method handleEvent is an old AWT thing, right? You need to learn how to use Swing properly:

      This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

      Comment

      Working...