繁体   English   中英

java int toString() 错误

[英]java int toString() error

嘿,我正在编写一个程序来计算文本文件的单词、句子、元音、字符......等。 我想使用 toString() 连接所有类和结尾并以以下格式返回它们: {filename}: {character count}c {vowel count}v {word count}w {line count}l {sentence count }s. 我在使用 int 类时遇到了问题。 Ex (charCount.toString())'c' + .... 到目前为止,这是我的代码。 谢谢

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

  }

}

更简单的一种:更改此方法如下,它会起作用(由于字符串连接,在此期间非字符串会自动转换为字符串。

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

或者,将int (它是整数的原始数据类型,因此没有像toString()这样的方法)替换为Integer (它是整数的 Java 类,将原始int包装在其中,并且由于它是一个类,因此它具有像toString() )这样的方法在你的代码中无处不在,你会很好。

例如替换:

private int sentenceCount;

和:

private Integer sentenceCount;

还将方法的返回类型从int更改为Integer

int 是 Java 中的原始类型,因此没有与之关联的方法。 但是你不需要使用toString()这应该可以正常工作return this.getfilePath() +':' + sentenceCount + 's' + wordCount + 'w';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM