943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6281
  • Java RSS
Aug 15th, 2004
0

I can't implement a word count into my text editor (JAVA)

Expand Post »
/**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();
}
}
}*/
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
michelle13 is offline Offline
1 posts
since Aug 2004
Aug 15th, 2004
0

Re: HELP!! I can't implement a word count into my text editor (JAVA)

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)?

Java Syntax (Toggle Plain Text)
  1. //else if ("Word Count".equals(actionCommand))
  2. // count();
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: last section of code has to a static class
Next Thread in Java Forum Timeline: help needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC