Results 1 to 4 of 4
  1. #1
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Java Word Count Issue

    I'm relatively new to java and my problem is this.
    I'm currently writing code which will read a file and must analyse and display a number of different attributes of this file. As of yet I have it counting words, characters and lines. Lines and characters are fine but the word count gets the wrong value because there is an alteration that must be made as to what describes a word.
    My understanding is that the default description for a word is a combination of characters followed by a space is a word. I need it to register words as this: any combination of letters that are broken by a space OR a symbol.

    Eg. Word word wor-d w-ord.
    This would currently be 4 words with my count. I need it to display 6. Where w-ord or wor-d are 2 words respectively because of the dash symbol. The following is my code as it stands:
    The line and char count are fine, it's the word count I need to alter? Any help would be much appreciated! :)


    package wordAnalyzer;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    /**
    *
    * @author
    */
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;

    public class FileAnalyzer {
    //counts number of lines

    public int LineCount() throws Exception {
    File file = new File("testfile.txt");
    FileReader fileRead = new FileReader(file);
    BufferedReader reader = new BufferedReader(fileRead);

    LineNumberReader lineNum = new LineNumberReader(reader);
    int lineCount = 0;
    while (lineNum.readLine() != null) {
    lineCount++;
    }
    return lineCount;
    }

    //counts number of words
    public int WordCount() throws Exception {
    File file = new File("testfile.txt");
    FileReader fileRead = new FileReader(file);
    BufferedReader reader = new BufferedReader(fileRead);

    String line = "";
    String string = "";
    int wordCount = 0;
    while ((line = reader.readLine()) != null) {
    string += line + " ";
    }
    StringTokenizer token = new StringTokenizer(string);
    while (token.hasMoreTokens()) {
    String s = token.nextToken();
    wordCount++;
    }
    return wordCount;
    }

    public void SameWordCount() {
    }

    public long CharacterCount() throws Exception {
    File file = new File("testfile.txt");
    FileReader fileRead = new FileReader(file);
    BufferedReader reader = new BufferedReader(fileRead);

    long numberOfChars = file.length();
    long charCount = 0;

    while (charCount < file.length()) {
    charCount++;
    }
    return charCount;
    }

    public static void main(String[] args) throws Exception {
    FileAnalyzer analysis = new FileAnalyzer();

    int menuChoice = 0;
    do {
    String menu =
    JOptionPane.showInputDialog("Please enter a number from 1 - 4\n"
    + "(1)Word Count\n(2)Character Count\n(3)Line Count\n(4)Exit");
    menuChoice = Integer.parseInt(menu);

    if (menuChoice == 1)//Word Count
    {
    JOptionPane.showMessageDialog(null, "Number Of Words: " + analysis.WordCount());
    } else if (menuChoice == 2)//Character Count
    {
    JOptionPane.showMessageDialog(null, "Number Of Characters: " + analysis.CharacterCount());
    } else if (menuChoice == 3)//Line Count
    {
    JOptionPane.showMessageDialog(null, "Number Of Lines: " + analysis.LineCount());
    } else if (menuChoice == 4)
    {
    System.exit(0);
    } else if (menuChoice < 0 || menuChoice > 4)//close system for incorrect values
    {
    JOptionPane.showMessageDialog(null, "Incorrect Value.\n"
    + "Please Insert a Value from 1 - 4", "Alert",
    JOptionPane.ERROR_MESSAGE);
    }
    } while (menuChoice != 4);
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Java Word Count Issue

    Please wrap your code in [code] tags [/code] when posting code so it retains its formatting in the forum.
    Please do not ask for code as refusal often offends.

  3. #3
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Word Count Issue

    Apologies, I haven't used this much, how do I wrap the code?

  4. #4
    Fluxtime is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Java Word Count Issue

    ok, i have wrapped the code and here it is: hopefully its easier to read now?

    Java Code:
       package fileAnalyzer;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author
     */
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class FileAnalyzer {
        //counts number of lines
    
        public int LineCount() throws Exception {
            File file = new File("testfile.txt");
            FileReader fileRead = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileRead);
    
            LineNumberReader lineNum = new LineNumberReader(reader);
            int lineCount = 0;
            while (lineNum.readLine() != null) {
                lineCount++;
            }
            return lineCount;
        }
    
        //counts number of words
        public int WordCount() throws Exception {
            File file = new File("testfile.txt");
            FileReader fileRead = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileRead);
    
            String line;
            String string = "";
            int wordCount = 0;
            while ((line = reader.readLine()) != null) {
                string += line + " ";
            }
            StringTokenizer token = new StringTokenizer(string);
            while (token.hasMoreTokens()) {
                String s = token.nextToken();
                wordCount++;
            }
            return wordCount;
        }
    
        public void SameWordCount() {
        }
    
        public long CharacterCount() throws Exception {
            File file = new File("testfile.txt");
            
            long charCount = 0;
    
            while (charCount < file.length()) {
                charCount++;
            }
            return charCount;
        }
    
        public static void main(String[] args) throws Exception {
            FileAnalyzer analysis = new FileAnalyzer();
            
            int menuChoice = 0;
            do {
                String menu =
                        JOptionPane.showInputDialog("Please enter a number from 1 - 4\n"
                        + "(1)Word Count\n(2)Character Count\n(3)Line Count\n(4)Exit");
                menuChoice = Integer.parseInt(menu);
    
                if (menuChoice == 1)//Word Count
                {
                    JOptionPane.showMessageDialog(null, "Number Of Words: " + analysis.WordCount());   
                } else if (menuChoice == 2)//Character Count
                {
                    JOptionPane.showMessageDialog(null, "Number Of Characters: " + analysis.CharacterCount());
                } else if (menuChoice == 3)//Line Count
                {
                    JOptionPane.showMessageDialog(null, "Number Of Lines: " + analysis.LineCount());
                } else if (menuChoice == 4) 
                {
                    System.exit(0);
                } else if (menuChoice < 0 || menuChoice > 4)//close system for incorrect values
                {
                    JOptionPane.showMessageDialog(null, "Incorrect Value.\n"
                            + "Please Insert a Value from 1 - 4", "Alert",
                            JOptionPane.ERROR_MESSAGE);
                }
            } while (menuChoice != 4);
        }
    }

Similar Threads

  1. Replies: 6
    Last Post: 04-25-2012, 09:17 PM
  2. Word Count That Ignores Punctuation And Space
    By stinson in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 04:20 AM
  3. Word Count in Java
    By michaelangelokrk in forum New To Java
    Replies: 4
    Last Post: 09-22-2011, 08:34 AM
  4. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 09:21 AM
  5. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •