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
- import java.awt.*;
- import java.io.*;
- import java.util.*;
- import java.util.ArrayList;
- import java.awt.event.*;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- import javax.swing.JOptionPane;
- public class Task2a1 extends javax.swing.JFrame {
- 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(Color.black);
- //button panel
- Panel buttons = new Panel();
- buttons.add(btnLoad=new Button("Load File"));
- buttons.add(btnLoadA=new Button("Load ArrayList"));
- buttons.add(btnExit=new Button("Exit"));
- btnLoad.setBackground(Color.red);
- btnLoadA.setBackground(Color.red);
- btnExit.setBackground(Color.red);
- add("South",buttons);
- //label panel
- Panel label = new Panel();
- label.setBackground(Color.gray);
- label.add(lab = new Label("STRING PROCESSING"));
- add("North",label);
- //textarea panel
- Panel textarea = new Panel();
- setBackground(Color.black);
- textarea.add(txttext= new TextArea(10,75));
- textarea.add(list1 = new TextArea(10,75));
- add("Center",textarea);
- pack();
- resize(1024,768);
- show();
- }
- public boolean handleEvent(Event event) {
- //writing to file
- if(event.id==Event.ACTION_EVENT){
- if(event.target instanceof Button){
- if("Load File".equals(event.arg)){
- String str="";
- try{
- BufferedReader in = new BufferedReader(new FileReader("English.txt"));
- while ((str = in.readLine()) != null) {
- txttext.append(str);
- }
- in.close();
- }
- catch (IOException y) {
- txttext.setText("INVALID FILE");
- }
- }
- else if(event.id==Event.ACTION_EVENT){
- if(event.target instanceof Button){
- if("Exit".equals(event.arg)){
- setVisible(false);
- }
- }
- }
- else if(event.id==Event.ACTION_EVENT){
- if(event.target instanceof Button){
- if("Load ArrayList".equals(event.arg)){
- ArrayList<String> input1 = new ArrayList<String>();
- ArrayList<String> output = new ArrayList<String>();
- String delim1 = " -,.'";
- StringTokenizer st = new StringTokenizer(txttext.getText(), delim1);
- while (st.hasMoreTokens()) {
- System.out.println(st.nextToken());
- // input1.add(st.nextToken());
- }
- int n = input1.size();
- for(int i = 0; i < n ; i++)
- System.out.println( input1.get( i ) );
- }
- }
- }
- }
- }
- return true;
- }
- }