Please Help on my Assignment for Computer structure ECPLISE. THANK YOU . Assume
ID: 3764634 • Letter: P
Question
Please Help on my Assignment for Computer structure ECPLISE. THANK YOU
. Assume that the document file has no formatting, except indication of end - of - line.The program should allow for selecting a file to be processed. This can be done through a visual interface displaying a file tree otherw ise indicate to the user where the program is looking for the file (an absolute path or a relative path to the directory where the program is running).
Define an IndexTree class such that each node has data fields to store a word, the count of occurences of that word in a document file, and the line number for each occurrence. Use an ArrayList to store the line numbers. Use an IndexTree object to store an index of words appearing in a text file, and then display the index by performing an inorer traversal of this tree
Explanation / Answer
package com.javacodegeeks.snippets.desktop;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.StringTokenzier;
public class SelectFileSelectionModeInFileChooser {
private static final long serialVersionUID = 1L;
private static void createAndShowGUI() {
// Create and set up the window.
final JFrame frame = new JFrame("Centered");
// Display the window.
frame.setSize(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("Choose file/directory");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createFileChooser(frame);
}
});
frame.getContentPane().add(button);
}
private static void createFileChooser(final JFrame frame) {
String filename = File.separator+"tmp";
JFileChooser fileChooser = new JFileChooser(new File(filename));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // only directories
// fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.showOpenDialog(frame);
System.out.println("File to open: " + fileChooser.getSelectedFile())
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class NumberWords
{
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("c:/test.txt");
BufferedReader br = new BufferedReader(fr);
String line = "", str = "";
int a = 0;
int b = 0;
while ((line = br.readLine()) != null) {
str += line + " ";
b++;
}
System.out.println("Totally " + b + " lines");
System.out.println(str);
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String s = st.nextToken();
a++;
}
System.out.println("File has " + a + " words are in the file");
}
}