| | |
I can't implement a word count into my text editor (JAVA)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2004
Posts: 1
Reputation:
Solved Threads: 0
/**I commented everything out dealing with the word count so it works. The problem is that I am not able to read what the file name is. It does not catch the file name, but it prints out the contents of it when it goes to the catch **/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class FileDialogDemo extends JFrame implements ActionListener
{
private JMenuItem jmiOpen, jmiSave, jmiExit, jmiAbout, jmiSpell, jmiReplace, jmiWCount, jmiHelp_me;
private JTextArea jtaFile= new JTextArea();
private JTextField jtfFilename=new JTextField(12);
private JLabel jlblStatus =new JLabel();
private JFileChooser jFileChooser =new JFileChooser();
public static void main(String []args)
{
FileDialogDemo frame=new FileDialogDemo();
frame.setSize(500, 500);
frame.setVisible(true);
}
public FileDialogDemo()
{
setTitle("Test JFileChooser ");
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu EditMenu = new JMenu("Edit");
mb.add(EditMenu);
JMenu HelpMenu = new JMenu("Help");
mb.add(HelpMenu);
fileMenu.add(jmiOpen = new JMenuItem("Open"));
fileMenu.add(jmiSave = new JMenuItem("Save"));
fileMenu.add(jmiExit = new JMenuItem("Exit"));
fileMenu.add(jmiAbout = new JMenuItem("About"));
EditMenu.add(jmiReplace = new JMenuItem("Replace"));
EditMenu.add(jmiSpell = new JMenuItem("Spell"));
EditMenu.add(jmiWCount = new JMenuItem("Word Count"));
HelpMenu.add(jmiHelp_me=new JMenuItem("Help"));
//jHelpChooser.setCurrentDirectory(new Help("."));
getContentPane().add(new JScrollPane(jtaFile),
BorderLayout.CENTER);
getContentPane().add(jlblStatus, BorderLayout.SOUTH);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiAbout.addActionListener(this);
jmiExit.addActionListener(this);
jmiHelp_me.addActionListener(this);
jmiWCount.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand =e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
{
if("Open".equals(actionCommand))
open();
else if("Save".equals(actionCommand))
save();
else if("About".equals(actionCommand))
{
JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs", "About this Demo", JOptionPane.INFORMATION_MESSAGE);
}
else if("Exit".equals(actionCommand))
System.exit(0);
else if("Help".equals(actionCommand))
JOptionPane.showMessageDialog(null, "YOU ARE STUPID IF YOU NEED TO USE HELP");
//else if ("Word Count".equals(actionCommand))
// count();
}
}
private void open()
{
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
open(jFileChooser.getSelectedFile());
}
}
private void open(File file)
{
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jtaFile.append(new String(b, 0, b.length));
in.close();
jlblStatus.setText(file.getName() + " Opened");
}
catch (IOException ex)
{
jlblStatus.setText("Error opening " + file.getName());
}
}
private void save()
{
if(jFileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
save(jFileChooser.getSelectedFile());
}
}
private void save(File file)
{
try{
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file));
byte[] b = (jtfFilename.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
jlblStatus.setText(file.getName() + " Saved ");
}
catch(IOException ex)
{
jlblStatus.setText("Error saving " + file.getName());
}
}
/** private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}
private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}*/
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class FileDialogDemo extends JFrame implements ActionListener
{
private JMenuItem jmiOpen, jmiSave, jmiExit, jmiAbout, jmiSpell, jmiReplace, jmiWCount, jmiHelp_me;
private JTextArea jtaFile= new JTextArea();
private JTextField jtfFilename=new JTextField(12);
private JLabel jlblStatus =new JLabel();
private JFileChooser jFileChooser =new JFileChooser();
public static void main(String []args)
{
FileDialogDemo frame=new FileDialogDemo();
frame.setSize(500, 500);
frame.setVisible(true);
}
public FileDialogDemo()
{
setTitle("Test JFileChooser ");
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu EditMenu = new JMenu("Edit");
mb.add(EditMenu);
JMenu HelpMenu = new JMenu("Help");
mb.add(HelpMenu);
fileMenu.add(jmiOpen = new JMenuItem("Open"));
fileMenu.add(jmiSave = new JMenuItem("Save"));
fileMenu.add(jmiExit = new JMenuItem("Exit"));
fileMenu.add(jmiAbout = new JMenuItem("About"));
EditMenu.add(jmiReplace = new JMenuItem("Replace"));
EditMenu.add(jmiSpell = new JMenuItem("Spell"));
EditMenu.add(jmiWCount = new JMenuItem("Word Count"));
HelpMenu.add(jmiHelp_me=new JMenuItem("Help"));
//jHelpChooser.setCurrentDirectory(new Help("."));
getContentPane().add(new JScrollPane(jtaFile),
BorderLayout.CENTER);
getContentPane().add(jlblStatus, BorderLayout.SOUTH);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiAbout.addActionListener(this);
jmiExit.addActionListener(this);
jmiHelp_me.addActionListener(this);
jmiWCount.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand =e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
{
if("Open".equals(actionCommand))
open();
else if("Save".equals(actionCommand))
save();
else if("About".equals(actionCommand))
{
JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs", "About this Demo", JOptionPane.INFORMATION_MESSAGE);
}
else if("Exit".equals(actionCommand))
System.exit(0);
else if("Help".equals(actionCommand))
JOptionPane.showMessageDialog(null, "YOU ARE STUPID IF YOU NEED TO USE HELP");
//else if ("Word Count".equals(actionCommand))
// count();
}
}
private void open()
{
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
open(jFileChooser.getSelectedFile());
}
}
private void open(File file)
{
try{
BufferedInputStream in =new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jtaFile.append(new String(b, 0, b.length));
in.close();
jlblStatus.setText(file.getName() + " Opened");
}
catch (IOException ex)
{
jlblStatus.setText("Error opening " + file.getName());
}
}
private void save()
{
if(jFileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
save(jFileChooser.getSelectedFile());
}
}
private void save(File file)
{
try{
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file));
byte[] b = (jtfFilename.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
jlblStatus.setText(file.getName() + " Saved ");
}
catch(IOException ex)
{
jlblStatus.setText("Error saving " + file.getName());
}
}
/** private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}
private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}*/
}
Please put in [ CODE ] Tags [ /CODE ] around your code and post the program with errors so that I can see what actually is wrong.
But right off the bat I notice this mistake. Did you mean to do count(filename)?
But right off the bat I notice this mistake. Did you mean to do count(filename)?
Java Syntax (Toggle Plain Text)
//else if ("Word Count".equals(actionCommand)) // count();
![]() |
Similar Threads
- Help with find and replace dialog in text editor application! (Java)
- Java Text Editor (Scroll Bars) (Java)
- Word count help. (C++)
- Text Editor-Help (C++)
- Can you please help me write this word count program in another way (Python)
- word count in borland c++ ?? (C++)
Other Threads in the Java Forum
- Previous Thread: last section of code has to a static class
- Next Thread: help needed
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working





