423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

String processing

P: 12
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:
Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*;
  2.    import java.io.*;
  3.    import java.util.*;
  4.    import java.util.ArrayList;
  5.    import java.awt.event.*;
  6.    import java.io.BufferedReader;
  7.    import java.io.FileReader;
  8.    import java.io.IOException;
  9.    import java.util.ArrayList;
  10.    import java.util.StringTokenizer;
  11.    import javax.swing.JOptionPane;
  12.  
  13.     public class Task2a1 extends javax.swing.JFrame {
  14.       Button btnLoad;
  15.       Button btnLoadA;
  16.       Button btnExit;
  17.       Label lab;
  18.       TextArea txttext;
  19.       TextArea list1;
  20.        public static void main(String args[])throws IOException {
  21.          Task2a1 app = new Task2a1();
  22.       }
  23.        public Task2a1() {
  24.          super("STRING PROCESSING");
  25.          setBackground(Color.black);
  26.  
  27.           //button panel
  28.          Panel buttons = new Panel();
  29.          buttons.add(btnLoad=new Button("Load File"));
  30.          buttons.add(btnLoadA=new Button("Load ArrayList"));
  31.          buttons.add(btnExit=new Button("Exit"));
  32.          btnLoad.setBackground(Color.red);
  33.          btnLoadA.setBackground(Color.red);
  34.          btnExit.setBackground(Color.red);
  35.          add("South",buttons);
  36.  
  37.           //label panel
  38.          Panel label = new Panel();
  39.          label.setBackground(Color.gray);
  40.          label.add(lab = new Label("STRING PROCESSING"));
  41.          add("North",label);
  42.           //textarea panel
  43.          Panel textarea = new Panel();
  44.          setBackground(Color.black);            
  45.          textarea.add(txttext= new TextArea(10,75));
  46.          textarea.add(list1 = new TextArea(10,75));
  47.          add("Center",textarea);
  48.  
  49.          pack();
  50.          resize(1024,768);         
  51.          show();
  52.       }
  53.        public boolean handleEvent(Event event) {
  54.  
  55.           //writing to file
  56.          if(event.id==Event.ACTION_EVENT){
  57.             if(event.target instanceof Button){
  58.                if("Load File".equals(event.arg)){
  59.  
  60.                   String str=""; 
  61.                   try{
  62.                      BufferedReader in = new BufferedReader(new FileReader("English.txt"));
  63.                      while ((str = in.readLine()) != null) {
  64.                         txttext.append(str);          
  65.                      }
  66.                      in.close();
  67.  
  68.  
  69.                   } 
  70.                       catch (IOException y) { 
  71.                         txttext.setText("INVALID FILE");
  72.                      } 
  73.  
  74.                }
  75.                else if(event.id==Event.ACTION_EVENT){
  76.                   if(event.target instanceof Button){
  77.                      if("Exit".equals(event.arg)){  
  78.                         setVisible(false);
  79.                      }
  80.                   }
  81.                }
  82.                else if(event.id==Event.ACTION_EVENT){
  83.                   if(event.target instanceof Button){
  84.                      if("Load ArrayList".equals(event.arg)){  
  85.                         ArrayList<String> input1 = new ArrayList<String>();
  86.                         ArrayList<String> output = new ArrayList<String>();
  87.                         String delim1 = "   -,.'";
  88.                         StringTokenizer st = new StringTokenizer(txttext.getText(), delim1);  
  89.                         while (st.hasMoreTokens()) {
  90.                            System.out.println(st.nextToken());
  91.                           // input1.add(st.nextToken());
  92.                         }
  93.                         int n = input1.size(); 
  94.                         for(int i = 0; i < n ; i++) 
  95.                            System.out.println( input1.get( i ) ); 
  96.                      }
  97.                   }
  98.                }
  99.  
  100.             }
  101.          }
  102.  
  103.          return true;
  104.       }
  105.    }
Any ideas on how to go about this will highly be appreciated.
Apr 18 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 849
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.
Apr 18 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
Method handleEvent is an old AWT thing, right? You need to learn how to use Swing properly:

http://java.sun.com/docs/books/tutor...ing/index.html
Apr 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.