Author Topic: [J] Text Analyzer  (Read 163 times)

Ganon

  • Full Member
  • ***
  • Posts: 110

  • Activity
    0%
    • View Profile
    • Email
[J] Text Analyzer
« on: April 05, 2010, 08:21:48 PM »
First of all, this program is supposed to scan a text file and look at it's properties such as character, word, wordlength, etc COUNT.

I'm having some trouble running this program through Eclipse and directing it to run "java ProcessText testProg.txt". I have put the txt file all through my workspace project but Eclipse still can't find it.

Also I just want to make sure the logic is right here. The thing I'm worried about is it fully scanning a bulk of text in the file. Lastly, I'm not sure how to do my letter counting method so it outputs as a result. If I can figure that out, I can complete the character count method which wants to add all the letters used.

Anyways, here's the code:
Main:
Code: [Select]
import java.io.File;

/*
 * @author Sasa Rkman
 * This program analyzes text from a text file and outputs the properties of the file
 */

public class ProcessText {
public static void main(String[] args) {

        if (args.length == 0) {
            System.err.println("Usage: java CmdLineArgs <string> <int> <double>");
            System.exit(1);
        }

        for(int i = 0; i < args.length; i++){
String[] files = new String[args.length];
File obj = new File(args[i]);
    TextStatistics test = new TextStatistics(obj);
    System.out.println("The results for " + files);
    System.out.println(test);
        }
   
}
}
Methods:
Code: [Select]
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class TextStatistics {
private int words, lines;
private final int NUMCHARS = 26; //used in LetterCount
Scanner kbd = new Scanner(System.in);
private String userInput, input2;
private String[] output = new String[6]; //stores each part of analyzed data

public TextStatistics(File input){

Scanner kbd;

try {
kbd = new Scanner(input);
while(kbd.hasNext()){
output[0] = CharCount(userInput);
output[1] = WordCount(userInput);
output[2] = LineCount(userInput);
output[3] = wLengthCount(userInput);
output[4] = avgWLengthCount(userInput);
output[5] = LetterCount(userInput);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

//this counts the word length
private String wLengthCount(String input) {
int[] count = new int[12];
StringTokenizer tokenizer = new StringTokenizer(input, " , .;:'\"&!?-_\n\t12345678910[]{}()@#$%^*/+-");
String next = tokenizer.nextToken();

while(tokenizer.hasMoreTokens()) {
if(next.length() == 0){
count[0]++;
}
if(next.length() == 1){
count[1]++;
}
if(next.length() == 2){
count[2]++;
}
if(next.length() == 3){
count[3]++;
}
if(next.length() == 4){
count[4]++;
}
if(next.length() == 5){
count[5]++;
}
if(next.length() == 6){
count[6]++;
}
if(next.length() == 7){
count[7]++;
}
if(next.length() == 8){
count[8]++;
}
if(next.length() == 9){
count[9]++;
}
if(next.length() == 10){
count[10]++;
}
if(next.length() ==11){
count[11]++;
}
if(next.length() == 12){
count[12]++;
}
}
for(int i=0; i<13; i++){
return Integer.toString(count[i]);
}
return "";
}

//this counts the average word length
private String avgWLengthCount(String input) {
int average = Integer.parseInt(wLengthCount(input)) / Integer.parseInt(WordCount(input));
return Integer.toString(average);
}

//this counts all the lines in the text
private String LineCount(String input) {
lines++;
return Integer.toString(lines);
}

//this counts all the total words
private String WordCount(String input) {

StringTokenizer tokenizer = new StringTokenizer(input, " , .;:'\"&!?-_\n\t12345678910[]{}()@#$%^*/+-");
words += tokenizer.countTokens();
return Integer.toString(words);

}

//this counts all the total characters
private String CharCount(String input) {
char character;
return null;
}

//this shows all the letters/characters used
public String LetterCount(String input){
int[] upper = new int[NUMCHARS];
int[] lower = new int[NUMCHARS];

char current; //the current character being looked at
int other = 0; //non-alphabetics

input = kbd.nextLine();

//count each letter
for(int ch = 0; ch< input.length(); ch++){
current = input.charAt(0);
if (current >= 'A' && current <= 'Z')
upper[current-'A']++;
else
if(current >= 'a' && current <= 'z')
lower[current-'a']++;
else
other++;
}

//print results
System.out.println();
for(int letter=0; letter < upper.length; letter++){
System.out.print( (char)(letter+'A'));
System.out.print(": " + upper[letter]);
System.out.print("\t\t" + (char)(letter + 'a'));
System.out.println(": " + lower[letter]);
}
System.out.println();
return "Non alphabetic characters: " + other;
}
public String toString(){
for(int i = 0; i < 6; i++){
System.out.println(output[i]);
}
return input2;

}
}

Jared

  • Lead Programmer
  • Administrator
  • Jr. Member
  • *****
  • Posts: 82
  • Programmer

  • Activity
    0%
    • MSN Messenger - wardenmaster@gmail.com
    • AOL Instant Messenger - LinkUrHero
    • View Profile
    • Dual Solace
    • Email
Re: [J] Text Analyzer
« Reply #1 on: April 06, 2010, 10:04:02 PM »
I have the same problem with eclipse occasionally, if you want to run it using a text file as an input parameter, your better to compile it using a command prompt. That aside, a line that might very well help you with everything you are doing...

I'm just psudo-coding this, because it's late...
Code: [Select]
for(int count = 0; count < input.length; count++){
    if(input.charAt(count) == /*something you want to check it against*/)
         //do something special about it
}

By using something smiler to this code, you can just iterate through the entire input character by character and check it against cases (for example, checking it any character is a letter or a number).

Also, in case you don't know how to run/compile your program using command prompt, locate it and use these commands.

javac (filename).java
java (filename) (inputfile.whatever)
"Death smiles on us all, the only thing we can do is smile back."