Java Programming help--> How do I change this program to do this task. Thanks!

CM4

Member
Jun 9, 2001
64
0
0
Hi,

I have a java task to complete and I was hoping if some kind person can give me some help in completing it. I have been given a simple text editor program.
I need to add a search button to it, that will automatically run a method that searches a loaded textfile and finds the number of occurrences for the following words in an loaded text file:

The / the

of

program

components

GUI


I have made the search button but what code do I need to put into the search buttons method to make it search for the above words in any loaded text file.

The text file will contain the following text:

"Perhaps the most interesting feature of the editor program is how much of the behaviour is handled by the GUI components. The program code presented essentially creates the components and fits them together with some ?glue? code. Very little of the complicated activity of displaying and editing text needs to be programmed by the programmer, instead pre-built components are plugged together and a useful program quickly constructed."



I appreciate any help that can be offered. :)

Here is the code:

_________________________________________________________________________________________________

//Example Swing program to illustrate a very simple text editor
//Need to import classes from all these packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class VerySimpleEditorTwo extends JFrame {
//Instance variables for the GUI components used.
//Declaring the variables here allows them to be
//used by any instance methods in the class.
JPanel topPanel;
JPanel buttonPanel;
JPanel editorPanel;
JButton loadButton;
JButton saveButton;
JButton cutButton;
JButton copyButton;
JButton pasteButton;
//new-------------------> New changes
JButton basicsearchButton;
JScrollPane scroller;
JTextArea editor;
//The constructor build the window using a utility
//method and displays it.
public VerySimpleEditorTwo() {
//Create and configure the various panels
topPanel = new JPanel ();
topPanel.setLayout (new BorderLayout ());
editorPanel = new JPanel ();
editorPanel.setLayout(new BorderLayout ());
editorPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
buttonPanel = new JPanel ();
buttonPanel.setBorder(BorderFactory.createEtchedBorder ());
//The panel containing the buttons uses a GridLayout of 1 column
//and 5 rows to position the buttons in a column.
GridLayout grid = new GridLayout ();
grid.setColumns(1);
grid.setHgap(10);
grid.setRows(5); //Set a small gap between the buttons
grid.setVgap(3); //to improve the appearance
buttonPanel.setLayout(grid);
scroller = new JScrollPane();
//The editor component handles all text display and editing. It
//is given a width of 40 characters.
editor = new JTextArea ();
editor.setColumns(40);
//Now create the buttons, giving each a listener to respond to
//the button being clicked. The listener methods delegate to
//private helper methods later in the class, to avoid the
//constructor method becoming too long and unwieldy.

//----------->Added Search Button
basicsearchButton = new JButton("Basic Search...");
basicsearchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
basicsearchFile();
}
});
/////////////////////////////////////////////////////
loadButton = new JButton("Load...");
loadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile();
}
});
saveButton = new JButton("Save...");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
cutButton = new JButton("Cut");
cutButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
});
copyButton = new JButton("Copy");
copyButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
});
pasteButton = new JButton("Paste");
pasteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
});
//Configure the top-level JFrame
setTitle("Very simple editor V2.0 by Ranatech Corporation TM");
getContentPane().add(topPanel, BorderLayout.CENTER);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Now assemble the components
topPanel.add(buttonPanel, BorderLayout.WEST);
buttonPanel.add(loadButton);
buttonPanel.add(basicsearchButton);
buttonPanel.add(saveButton);
buttonPanel.add(cutButton);
buttonPanel.add(copyButton);
buttonPanel.add(pasteButton);
topPanel.add(editorPanel, BorderLayout.CENTER);
editorPanel.add(scroller, BorderLayout.CENTER);
scroller.getViewport().add(editor);
pack();
setVisible(true);
}
//The following methods implement the button click behaviour.
//Display a file dialog and load a file.
private void loadFile() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//If a file was selected, try and load it.
File file = fc.getSelectedFile();
try {
editor.read(new FileReader(file), null);
}
catch (IOException exp) {}
}
}
//Display a save file dialog and save the current file.
private void saveFile() {
JFileChooser fc = new JFileChooser ();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
editor.write(new FileWriter(file));
}
catch (IOException exp) {}
}
}

//Searches an imported file for selected words
private void basicsearchFile() {

// WHAT CODE TO PUT HERE?
// A JOptionPane message box, displaying the words and the number of times they appear in the loaded text file would be great.
// The words the code must look for are: The / the , of , program , components, GUI

}


//Cut, copy and paste are implemented using methods provided by a
//superclass of the JTextArea class, and work with the system
//clipboard. Very useful and makes it very easy to implement
//cut/copy/paste!!
//
//The requestFocus method is used to make the JTextArea the active
//component after a button is clicked. The component with the focus
//receives the input events. If focus is not returned to the
//JTextArea it remains with the button, preventing text being
//entered into the text area until it is clicked on to regain
//focus.
private void copy() {
editor.copy();
editor.requestFocus();
}
private void paste() {
editor.paste();
editor.requestFocus();
}
private void cut() {
editor.cut();
editor.requestFocus();
}
public static void main(final String[] args) {
final VerySimpleEditorTwo editor = new VerySimpleEditorTwo ();
}
}


_____________________________________________

Thanks in advance
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
I'm assuming that you have access to a String representation ( from eather a file or a text area ). Once you get the it in a string form you can follow my example below

string - string you wish to search
search - values to search for
count - number of occurences of each item in the search array


String string = "quick brown fox jumped over a lazy dog slow blue fox jumped over a lazy dog very lazy very quick ";
String [] search = { "quick", "fox", "lazy", " " };
int [] count = new int[4];

int position = -1;
for(int j=0; j<search.length; j++) {
do {
if((position = string.indexOf(search[j], ++position)) >= 0)
++count[j];
} while(position != -1);
}


edit: i keep forgeting that [ i ] gets swallowed by the page parsers :eek:. I replaced them with [j]
 

CM4

Member
Jun 9, 2001
64
0
0
Thanks, but I am new to all this GUI stuff. How do I convert the imported text file into a String that I can then manipulate?

 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
you can grab it from the JTextArea component ( I believe you are loading it with the text file ) via the getText( ) method
 

oLLie

Diamond Member
Jan 15, 2001
5,203
1
0
He's not printing the text file onto the GUI I don't think. Look into FileChooser or FileLoader. Once you've checked that stuff out look at FileReader, and BuffereReader. Hope that helps,
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< He's not printing the text file onto the GUI I don't think. Look into FileChooser or FileLoader. Once you've checked that stuff out look at FileReader, and BuffereReader. >>



yes he is, look at the line editor.read(new FileReader(file), null);
 

CM4

Member
Jun 9, 2001
64
0
0
To:

HigherGround

What does this code mean in the example you have given me?

if((position = string.indexOf(search[j], ++position)) >= 0)

thanks
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< if((position = string.indexOf(search[j], ++position)) >= 0) >>



let's brake it down ...

string.indexOf(String s, int p) returns an index of of substring s at position equal or greater than index p. For example given String s = "red blue red" and call s.indexOf("red", 0) would return index 0, since this is the first occurance of substring red in string s, however a call s.indexOf("red", 1) would return 9 since the first substring "red" starts at index 0 and we are starting a search at index 1, hence the second occurance is returned.

That is why the call involves incrementing the position index ( ++position ), that way we return the next occurance of the searched substring, until we hit the end when the call returns -1, at which point we are done.
 

CM4

Member
Jun 9, 2001
64
0
0
I have been trying to make a JOptionPane message dialog box that will show the results of the search.

This is how I would like it to be laid out;

Search Results:

Number of Instances:
The: <the number of times found>
the: <the number of times found>
of: <the number of times found>
program: <the number of times found>
components: <the number of times found>
GUI: <the number of times found>

Thanks :)

 

CM4

Member
Jun 9, 2001
64
0
0
I have done it now. Thanks for the help.

But I will need to make a different version of the Editor.

This is what should happen.
User loads a text file.---> user the click on the search button --> A window appears with 3 Text Boxes --> User enters a word into each of the 3 textboxes --> program searches and finds the number of instancesof the input words in the loade text file.


Any help would be appreciated. Its all about expanding this older version.

Thankyou :)
 

CM4

Member
Jun 9, 2001
64
0
0
hi, I need help with the third version of the program.

See last post.

Thanks