473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String processing

12 New Member
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
2 2044
Laharl
849 Recognized Expert Contributor
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
1,216 Recognized Expert Top Contributor
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

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

Similar topics

13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: Paul H | last post by:
I have a text file that contains the following: ******************** __StartCustomerID_41 Name: Fred Smith Address: 57 Pew Road Croydon
6
by: Alexander Muylaert | last post by:
Hi Does anyone know a good starting point about high speed string processing in C#? What I need is a very fast routine for a case insensitive "contains". e == E == é == ë == ... Kind...
12
by: Joe Fallon | last post by:
I would like to know the syntax to check that an Object is a String. If it was a number test I might use IsNumeric. But there is no function: IsString (is there?) -- Joe Fallon
9
by: Joel Finkel | last post by:
Is there a way to execute a method if all we know is its name as a string? Let's say we have the following class. What is the code for the Execute method? I need a solution that works with the...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many...
5
by: mloraditch | last post by:
All, I have some C# code doing some form processing for me. Some forms have a certain control, some don't. If the control exists i need to do some processing on it later. Of course when i try to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.