Re: Java Word Count Issue
Please wrap your code in [code] tags [/code] when posting code so it retains its formatting in the forum.
Re: Java Word Count Issue
Apologies, I haven't used this much, how do I wrap the code?
Re: Java Word Count Issue
ok, i have wrapped the code and here it is: hopefully its easier to read now?
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);
}
}