Hey I am writing a program that calculates the words, sentences, vowels, characters..etc of a textfile. I want to use a toString() to concatenate all the classes and the end and return them in the format: {filename}: {character count}c {vowel count}v {word count}w {line count}l {sentence count}s. I am having trouble with the int classes. Ex (charCount.toString())'c'
+ .... Here is my code so far. Thanks
import java.io.FileInputStream;
public class TextFileAnalyzer {
public static void main(String[] args) {
try {
TextFileAnalyzer mytfa = new TextFileAnalyzer("/Users/patrickmro/Desktop/text.txt");
System.out.println(mytfa.getCharCount());
System.out.println(mytfa.getSentenceCount());
System.out.println(mytfa.getLineCount());
System.out.println(mytfa.getVowelCount());
}
catch (Exception e) {
}
}
// You'll probably want some private fields
private java.lang.String filePath;
private int sentenceCount;
private int wordCount;
private int charCount;
private int lineCount;
private int vowelCount;
private java.lang.String textString;
/**
* Constructs a new TextFileAnalyzer that reads from the given file. All file
* I/O is complete once this constructor returns.
* @param filePath a path to the file
* @throws Exception if any errors are generated by the Java API while reading
* from the file
*/
public TextFileAnalyzer(String filePath) throws Exception {
// This is your job...
this.filePath = filePath;
charCount = 0;
int prevChar = -1;
FileInputStream input = new FileInputStream(filePath);
for(int c = input.read(); c != -1 ; c = input.read()) {
charCount++;
if (c == '.' && prevChar != '.') {
sentenceCount++;
}
if(c == '\n') {
lineCount++;
}
if(Character.isWhitespace(c) && (prevChar != -1 || !Character.isWhitespace(prevChar) )) {
wordCount++;
}
prevChar = c;
if(c == 'A' || c == 'E' || c == 'I'|| c == 'O' || c == 'U') {
vowelCount++;
}
if(c == 'a' || c == 'e' || c == 'i'|| c == 'o' || c == 'u') {
vowelCount++;
}
}
lineCount++;
input.close();
}
/**
* Returns the number of single-byte characters in the file, including
* whitespace.
*
* @return the number of characters in the file
*/
public java.lang.String getfilePath() {
return this.filePath;
}
public int getSentenceCount() {
return sentenceCount ;
}
public int getWordCount() {
return wordCount ;
}
public int getCharCount() {
return charCount ;
}
public int getLineCount() {
return lineCount ;
}
public int getVowelCount() {
return vowelCount;
}
public java.lang.String toString() {
// {filename}: {character count}c {vowel count}v {word count}w {line count}l {sentence count}s
return (this.getfilePath())':' + (sentenceCount.toString())'s' + (wordCount.toString())'w';
}
}