-4

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';

  }

}
Gimby
  • 5,095
  • 2
  • 35
  • 47
javakook
  • 121
  • 1
  • 2
  • 12
  • 1
    You have an extra `return` in your `toString()` method. – azurefrog Oct 29 '15 at 18:54
  • Yeah but thats not the issue – javakook Oct 29 '15 at 18:55
  • 2
    As an aside, why are you fully qualifying `String` as `java.lang.String`? There's no reason to do this unless you've defined your own class called `String` (which you should never, ever do). – azurefrog Oct 29 '15 at 18:56
  • Well, you haven't mentioned what the issue is, so I'm just mentioning whatever I see, and since the title of your question talks about `toString()`... – azurefrog Oct 29 '15 at 18:56
  • Yeah..i took it out. Anyways how can I get my output in the correct format using toString()? {filename}: {character count}c {vowel count}v {word count}w {line count}l {sentence count}s – javakook Oct 29 '15 at 18:58
  • 1
    Please mention the explicit error in your post, not just an abbreviated form of the error in the title. Also please post only the relevant code. – rgettman Oct 29 '15 at 18:59
  • My mistake..I think it is pretty straightforward what I was asking... – javakook Oct 29 '15 at 19:00
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – javakook Oct 29 '15 at 19:19
  • Don't mangle the question, that will only net you more downvotes and possibly can help to lead to a question ban. I rolled it back. If you want to get rid of downvotes, consider improving the question instead. – Gimby Oct 29 '15 at 20:50

2 Answers2

2

Easier one: change this methods as follows and it will work (due to string concatenation, during which non-Strings are automatically converted to strings.

public java.lang.String toString() {
   return this.getfilePath() + ":" + sentenceCount + "s" + wordCount + "w";
}

Alternatively, replace int (which is the primitive data type for integers and hence does not have methods like toString()) with Integer (which is a Java class for integers, wrapping the primitive int inside it and since it is a class, it has methods like toString()) everywhere in your code and you would be good.

For example replace:

private int sentenceCount;

with:

private Integer sentenceCount;

Also change the return types of the methods from int to Integer.

Curious
  • 2,783
  • 3
  • 29
  • 45
1

int is a primitive type in Java and as such has no methods associated with it. However you do not need to use a toString() this should work fine return this.getfilePath() +':' + sentenceCount + 's' + wordCount + 'w';

user2644013
  • 192
  • 2
  • 14